SharePoint: Make a list field Required programmatically - sharepoint

I have a list and one of the list field is of managed meta data field type.It has to be a required field on the list.I would not set the Required attribute to true in the XML file, as other lists in the project use it as a optional field.Searched many articles, none of them helped .
Note: Field.Required=true;
Field.Update();
Doesn't make the field as required field
Field is not part of content type
Any ideas on this are appreciated..
Thanks

Is the field a part of a contenttype? In that case you might want to get a reference to the fieldlink and set that as required:
var myField = list.Fields["MyFieldName"];
var ct = list.Contenttypes["MyContentType"];
var fieldLink = ct.FieldLinks[myField];
fieldLink.Required = true;
ct.Update();

Try this
`
SPField field = list.Fields["MyField"];
field.Required= true;
field.Update();
list.Update();
`

Use sharepoint designer, select the list, a listing of columns will be displayed.
Double click the column and within the Column Editor popup uncheck "Allow blank Values?". Save and test...field should now be mandatory.

Related

How to get source list types of particular list/record field?

Here is I have two entity custom fields with list/record type,
custom_dev_j15 entity field has a custom source list (eg: one, two, three, four, etc)
custom_qa_v93 entity field has a standard source list as an object (eg: customer )
I've two vendor entity custom fields as stated in screenshots of question,
custentity473 --> customer is selected as list source
custentity474 --> custom_dev_j15_m_list as selected as list source ( which is custom list)
Here is snippet that i used to get the options of these fields,
// Snippet
var fieldDetails = {};
var record = nlapiCreateRecord("Vendor");
var field = record.getField("custentity473");
var selectoptions = field.getSelectOptions();
for ( var i in selectOptions) {
var Option = {
id : selectOptions[i].getId(),
label : selectOptions[i].getText()
}
Options.push(Option);
}
fieldDetail["options"] = Options;
But my need is to get source list information like name of the list source (customer or custom_dev_j15_m_list) via suitescript
any idea on how to get this information?
Thanks in advance
I'm not sure I understand this question for what you're trying to do.
In NetSuite almost always, you accommodate to the source list types because you know that's the type, and if you need something else (e.g. a selection which is a combination/or custom selection you'll use a scripted field)
Perhaps you can expand on your use case, and then we can help you further?

Read data in content type via CSOM

I have an issue with CSOM and content type, I used CSOM to read some value in a sharepoint list, it works well until I try to used content type If I create a new content type with my fields, it's worked but if I add my field in an existing content type. It doesn't work and that's return me the following error:
Error: Object reference not set to an instance of an object
Here a piece of code from where my error is comming:
List list = web.Lists.GetByTitle("myList");
CamlQuery query = CamlQuery.CreateAllItemsQuery();
ListItemCollection items = list.GetItems(query);
l_objCtx.Load(items, its => its.Include(item => item[ColumnName], item => item.Id));
l_objCtx.ExecuteQuery();
foreach (var item in items)
{
m_strGetprimary = item.Id.ToString();
return true;
}
I've done a lot of researches but I found nothing related to this issue
Any idea of what I'm doing wrong?
Thank you for your help
After adding SharePoint field to existing content type please check field name that you have just added. Sometimes it can be different and then your variable ColumnName might have wrong text value.
How to check it:
Go to site settings > Site content types > Your content type.
Select/click your field and in URL you will have param Field={field name}.

Sharepoint online default column value is set but not used

I want to set the default value of a sharepoint document library column.
I use the following code:
field = list.Fields.GetByTitle("fieldname");
field.DefaultValue = "the default value";
field.Update();
pnpClientContext.ExecuteQuery();
In the document library "Column default value settings" you can see that the default value is set.
But when adding a document to the library, the default value is not copied. This is caused by the fact that the option "use this default value" is not set using the code above (see screenshot)
"use default value" is not set
What i'm missing?
Also check it in Library Settings > General Settings > Column default value settings.
Click on desired Column name and set Use this default value.
So apparently you need to change the "Default Value" in two places. In the general library settings and in the specific column settings.
Hope that helps!
Here is a example for setting default values for list items.
The sample is from sharepoint 2010 but it should works for both sharepoint online and 2013.
ClientContext clientContext = new ClientContext(siteUrl);
Web web = clientContext.Web;
List list = web.Lists.GetByTitle("CustomList");
Field field = list.Fields.GetByTitle("Title");
field.DefaultValue = "Default";
field.Update();
clientContext.Load(field);
clientContext.ExecuteQuery();
You can try the following codeļ¼š
//get taxonomy field
var taxColumn = ctx.CastTo<TaxonomyField>(ctx.Web.Fields.GetByTitle(taxFieldTitle));
ctx.Load(taxColumn);
ctx.ExecuteQuery();
//initialize taxonomy field value
var defaultValue = new TaxonomyFieldValue();
defaultValue.WssId = -1;
defaultValue.Label = termLabel;
defaultValue.TermGuid = termId.ToString();
//retrieve validated taxonomy field value
var validatedValue = taxColumn.GetValidatedString(defaultValue);
ctx.ExecuteQuery();
//set default value for a taxonomy field
taxColumn.DefaultValue = validatedValue.Value;
taxColumn.Update();
ctx.ExecuteQuery();
Hope that helps!

Sharepoint 2010 Metadata fields Key, Value

I'm currently working with Sharepoint 2010 and Sharepoint API on creating a document library with some existing document lists.
I have created a WinForm that loops through a given doc lists and then add them to diffrent document libraries depending on 'Type(A metadata field)' of the document. Type is determined by reading the "Metadata fields" for the specific document. Metadata fields are read by creating Hashtable of SPFields
Question
When document metadata field is read to determin the 'Type', I have realised that the Metadatafield 'Type'(Key) actually pulls out as 'Type+TaxHTField0' and value for the Key pulls out as value|GUID
So for example if my Metadata field is called Doc_x0020_Type when it returns from the API it comes out as Doc_x0020_TypeTaxHTField0 the value for this should be just 'products' but it comes out as
products|21EC2020-3AEA-1069-A2DD-08002B30309D
Is there a setting we can set in sharepoint to eleminate adding extra charaters and GUID to both Key and Value for metadata fields?
Below is what I've done to rectify the issue, but wondered if it's a setting we can set in sharepoint
public String GetLibrary(Hashtable itemProperties)
{
String typeMetaField = "Doc_x0020_TypeTaxHTField0";
String sKey = String.Empty;
foreach (DictionaryEntry deEntry in itemProperties)
{
sKey = deEntry.Key.ToString();
if (sKey == typeMetaField){
_type = deEntry.Value.ToString();
string[] value = _type.Split('|');
_type = value[0].Trim();
}
}
return GetDocumentLibrary(_type);
}
This is by design.
If you add a taxonomy field to your own contenttype (for instance named 'MyTaxField') SharePoint will autogenerate a hidden 'Notes' field that contains the label and the guid of the values you select in the UI.
This is quite helpful when using SPSiteDataQuery since it will return empty values for taxonomy fields that allow multiple values (works with single value taxonomy fields though).
The only way to get the taxonomy values is to have a for the hidden field named 'MyTaxFieldTaxHTField0'.
But as you have discovered, this field might not be formatted as youd like :).
I have not tested this, but did you check if your contenttype contains a field called "Doc_x0020_Type" (possible of type TaxonomyFieldValue(Collection))?

Sharepoint Custom List with TimeStamp Field

I'm making a custom SharePoint List. I need a TimeStamp Field, but the only available type, by default, is DateTime.
Any help?
I think you would need to create a custom field type so that you can control the display of a DateTime type and validation etc - see this blog post for more info
I had the same problem in Sharepoint 2010 and solved it. Posting in case someone else finds this useful :)
To achieve this one must use the "Calculated" columntype.
From GUI:
Create new column
Pick type "Calculated".
Select "Created" column and add to formula.
Save.
From code:
As far as I can tell, there is two options to achieve this:
Access the "Created" and either set it's ShowInDisplayForm property to true or add the column to a view (for example the DefaultView).
Create a calculated column that points to the "Created" column, just as the GUI-example does. The trick is to set the "Formula" & the "OutputType" properties.
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists["test"];
string fieldName = list.Fields.Add("Timestamptest", SPFieldType.Calculated, false);
SPFieldCalculated field = list.Fields[fieldName] as SPFieldCalculated;
field.Formula = "=Created";
field.OutputType = SPFieldType.DateTime;
field.ShowInEditForm = false;
field.Update();
list.Update();
SPView defaultView = list.DefaultView;
defaultView.ViewFields.Add(field);
defaultView.Update();
}
}
});

Resources