Sharepoint 2010 - make Title, Description and Keyword fields as required fields in Picture Library using server-object-model - sharepoint

I'm creating a Sharepoint feature, this feature has an event receiver associated to it. In the event receiver, I'm creating a Document Library and Picture Library using server-side object model. I'm also adding new custom columns (around 80) to these newly created document and picture library. Now I want to modify the properties of the Description, Keywords and Title fields that are by default created along with the picture library. I want to make these fields as Required fields. How do I do this? I tried to set SPList.AllowContentTypes = true and try to change the attributes of these fields, but it doesn't work (neither gives an error nor makes these required fields). I also tried to access the content types and try to change the attributes using SPContentType.FieldsLinks["Column_name"].Required and SPContentType.Fields["Column_name"].Required but it gives me an error. Does anyone have any other suggestions?

Here is the answer....
SPContentType ct = mypiclib.ContentTypes["Picture"];
SPFieldLinks titleLink = ct.FieldLinks["Title"];
SPFieldLinks descLink = ct.FieldLinks["comments"]; //internal name of Description
SPFieldLinks keywords = ct.FieldLinks["keywords"];
titlelink.Required = true;
descLink.Required = true;
keywords.Required = true;
ct.Update();

can you tell us the error you got when trying to use the fieldlinks? Because this should work... I have done it like this:
SPContentType ct = web.Lists["*ListName*"].ContentTypes["*ContentTypeName*"];
SPFieldLinkCollection flinks = ct.FieldLinks;
flinks["*ColumnName*"].Required = true;
ct.update();

This should do the trick:
SPWeb yourWeb = ... //assign your web
SPList yourPictureLibrary = ... //assign your picture library
web.AllowUnsafeUpdates = true;
yourPictureLibrary.Fields[SPBuiltInFieldId.Title].Required = true;
yourPictureLibrary.Fields[SPBuiltInFieldId.Description].Required = true;
yourPictureLibrary.Fields[SPBuiltInFieldId.Keywords].Required = true;
yourPictureLibrary.Update();

SPAllowContentTypes isn't settable. You might try setting ContentTypesEnabled instead.
I don't have a 2010 box to test against, but if SPAllowContentTypes returns false I think you're looking at modifying the definition of your picture library in the 14 hive (TEMPLATE\FEATURES\PictureLibrary\PicLib) to get what you're after. Tread lightly there.

Related

Get FileDownloadUrl from attachment (Agile PLM)

I've been following the code samples included in Oracle document E15930_01 (Agile PLM Core Web Services User Manual). The samples are in Java, but I've translated what I need to .NET for the project I'm working on.
I can search for an object and return its attachments. I can get all the attachment properties except the one I need, fileDownloadUrl. This field is always blank.
Sample code follows. I thought by setting the property of allFiles to false and downloadUrl to true, I should get a download URL, but I don't. This code returns all the properties for the attachment except the one I want. Any thoughts on what I'm doing wrong?
AttachmentService svc = new AttachmentService();
svc.Credentials = credentials;
AgileGetFileAttachmentRequest[] req2 = InitializeArray<AgileGetFileAttachmentRequest>(1);
AgileFileAttachmentRequestType[] attachments = InitializeArray<AgileFileAttachmentRequestType>(1);
req2[0].classIdentifier = "MyIdentifier";
req2[0].objectNumber = "1234567890";
req2[0].allFiles = false;
req2[0].downloadUrl = true;
req2[0].attachments = attachments;
attachments[0] = new AgileFileAttachmentRequestType();
int rowId = getRowId(tt);
attachments[0].rowId = rowId;
GetFileAttachmentRequestType get = new GetFileAttachmentRequestType();
get.requests = req2;
GetFileAttachmentResponseType resp2 = svc.getFileAttachment(get);
AgileFileAttachmentResponseType[] attchResp = InitializeArray<AgileFileAttachmentResponseType>(1);
attchResp = resp2.responses[0].attachment;
Posting this in case someone else needs to do this or I need to do it later.
I found the data I needed. The download URLs are generated based on XML values in several fields in the database. They're the folder name, filename and FolderVersion on the row you're looking at. You need to parse the XML and retrieve the values to generate the link.
You can get the pattern for the download link through the Get Shortcut button.

Create Document Library using Client Object Model

Just a quick question, is it possible to create a Document Library using the Client Object Model in SharePoint 2010?
I know you can create lists etc. Am I right in saying that a Document Library is just a 'special' type of List?
How can I specify that the List I create will be a Document Library?
Any help will be appreciated
Thanks
Yes. You can specify the TemplateType for your List.
List of TemplateTypes
using (ClientContext clientCTX = new ClientContext(url))
{
ListCreationInformation lci = new ListCreationInformation();
lci.Description = "My own DocLib";
lci.Title = "Library";
lci.TemplateType = 101;
List newLib = clientCTX.Web.Lists.Add(lci);
clientCTX.Load(newLib);
clientCTX.ExecuteQuery();
}
You can also set the templatetype with the following code, which I think makes things clearer to new programmers:
lci.TemplateType = (int) ListTemplateType.DocumentLibrary;

How to add a Calculated field to AllContentType?

Today, I'm having a problem is after I had created a Calculated field. It seems there is no way to add AllContentTypes. And the DefaultView, maybe I can handle this. And I also saw this method:
spList.Fields.AddFieldAsXml(spFieldUser.SchemaXml, True, SPAddFieldOptions.AddToAllContentTypes);
But in this case, I'm not sure I can use it or not. Because my code is:
//SPField tempSPField = spList.Fields.CreateNewField(createSPColumnObject.ColumnType, createSPColumnObject.ColumnName);//We can not use this code line for creating Calculated (there is no constructor for this)
SPFieldCollection collFields = spList.Fields;
string strSPFieldCalculatedName = collFields.Add(createSPColumnObject.ColumnName, SPFieldType.Calculated, false);
if (createSPColumnObject.IsAddedToDefaultView)
{
SPView spView = spList.DefaultView;
spView.ViewFields.Add(strSPFieldCalculatedName);
spView.Update();
}
SPFieldCalculated spFieldCalculated = null;
//
spFieldCalculated = (SPFieldCalculated)collFields[createSPColumnObject.ColumnName];
spFieldCalculated.ShowInDisplayForm = true;
//spFieldCalculated.ShowInEditForm = true;
spFieldCalculated.ShowInListSettings = true;
//spFieldCalculated.ShowInNewForm = true;
spFieldCalculated.ShowInViewForms = true;
//
spFieldCalculated.Description = createSPColumnObject.ColumnDescription;
spFieldCalculated.Formula = string.Format(#"={0}",createSPColumnObject.CalcFormula);
spFieldCalculated.Update();
//spList.Fields.AddFieldAsXml(spFieldCalculated.SchemaXml, createSPColumnObject.IsAddedToDefaultView, SPAddFieldOptions.AddToAllContentTypes);// also use this code line because we will get an exception with a duplicate column ID.
spFieldCalculated.OutputType = SPFieldType.Text;
spList.Update();
I totally created a Calculated column but how can I add it to allcontent types ? everybody could help me out this ? BTW, to the DefaultView, I did like the above is right ? Could eveybody let me know this ?
I just worry about everybody get misunderstanding ? Or review with missing code. So could everybody please to take a look on my code clearly ? Thanks all.
Many thanks, :)
Standley Nguyen
I'm not sure if i fully understand what you are trying to do however i may be able to shed some light on some parts of what you are trying to do.
When you create your field does it then appear in your site actions -> site settings -> Site columns. If so you have created this correctly. If it doesn't there are hundreds of examples of how to do this if you search google.
Once you have your field create you then need to consider which content types you want to add it to. Once you have these content types you then have to add something called a field link to the Content type.
This isn't my code i have picked it off the web but this should do what you require.
SPContentType ct = web.ContentTypes[contentType];
ct.FieldLinks.Add(new SPFieldLink(field));
ct.Update();
Cheers
Truez

SharePoint 2010 BaseFieldControl Duplicates first item in list when in DisplayMode

I have come across a problem when using the BaseFieldControl that is driving me to distraction.
Essentinally I am trying to convert a list into a HTML table using the BaseFieldControl to render the fields.
When my table renders it writes out the correct number of lines BUT the data in each line is always the same as the first item in the list.
When I change the ControlMode property from SPControlMode.Display to SPControlMode.Edit the list renders correctly ( apart from being in Edit mode )
When my code running with ControlMode set to SPControlMode.Display I can actually get at the correct value in the BaseFieldControl.ItemFieldValue property but the wretched BaseFieldControl still insists on rendering the first item in the list!
I've also installed the web part on a SharePoint foundation and SharePoint 2010 server and I get the same results!
Finally I've googled around and found other peoples examples. Unfortunately when I try other dev's code ( unmodified ) I get exactly the same results!
This is what I'm doing. Any suggestions would be really appreciated!
foreach (string f in list.DefaultView.ViewFields)
{
TableCell c = new TableCell();
var i = item[f];
if (i != null)
{
SPField spf = item.Fields.GetField(f);
BaseFieldControl bfc = spf.FieldRenderingControl;
bfc.ControlMode = SPControlMode.Display;
bfc.Value = bfc.ItemFieldValue;
bfc.ID = Guid.NewGuid().ToString();
bfc.FieldName = spf.Title;
bfc.ListId = list.ID;
bfc.ItemId = item.ID;
SPContext context = SPContext.GetContext(this.Context, item.ID, list.ID, SPContext.Current.Web);
bfc.ItemContext = context;
bfc.RenderContext = context;
bfc.EnableViewState = true;
bfc.Visible = true;
c.Controls.Add(bfc);
}
else
{
c.Text = "NULL";
}
r.Cells.Add(c);
}
I finally fixed it. Turns out to be a problem with the SPWeb object. I had grabbed it from SPContext and passed it through as a reference to my method.
When I stopped doing that but instead created it within the method ( and created it once per item in the list ) it all worked fine.
Very strange.

Enterprise Keyword not updating in SharePoint 2010

Any idea how to inject values to the Enterprise Keywords column of a List / Doc Lib Item using code?
Tried the following, it didn't give any error, but that column wouldn't update, while the Title did.
using (var site = new SPSite("http://testweb"))
{
using (var web = site.OpenWeb("testsite1"))
{
var list = web.Lists["testlist1"];
var item = list.AddItem();
item["Title"] = string.Format("Injected from code on {0}", DateTime.Now.ToString());
item["Enterprise Keywords"] = "1;#Opera|4eed0518-9676-4afc-be20-9027b3b69e42";
item.Update();
}
}
In this code, Opera keyword has been added previously, I've checked it against the TaxonomyHiddenList list as well using code to extract the correct ID and IdForTerm (the GUID).
What am I missing here?
To add a taxonomy field value the approach is a little bit different. Please try:
TaxonomyField entKeyword = (TaxonomyField)item.Fields["Enterprise Keywords"];
TaxonomyFieldValue term = new TaxonomyFieldValue("1;#Opera|4eed0518-9676-4afc-be20-9027b3b69e42");
entKeyword.SetFieldValue(item,term);
in stead of:
item["Enterprise Keywords"] = "1;#Opera|4eed0518-9676-4afc-be20-9027b3b69e42";

Resources