Sharepoint online default column value is set but not used - sharepoint-online

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!

Related

How to set Option set value in CRM 2011?

I have an Option set in CRM 2011. It has four options:
Public
Private
Subsidiary
Other
Through plugin I want to set the value of this option set. Can anyone provide me the statement to set the value of this option set?
How to set optionsetvalue in plugins
In plugins you can write yourEntity.yourAttribute = new OptionSetValue(INDEX);
The INDEX is an int you can look up in your optionset editor (default values are several digit long).
OR
You set the optionset like yourEntity.Attributes.Add(“yourAttribute”, new OptionSetValue(INDEX));
You can set an option set value using the following:-
OptionSetValue myOptionSet = new OptionSetValue();
myOptionSet.Value = xxxx
myEntity.Attributes["optionSetAttributeName"] = myOptionSet;
// Where xxxx represents the value desired and can be checked on the attribute metadata page within the entity customisations
Whether 'myEntity' is actually preImage/postImage or just a dynamically created entity in the plug-in will determine whether you need to actually call the update method, but essentially this is the way you set the option set value and update the attribute.
I thought I'd share some code for handling option-sets in CRM here...
fieldValue = ((OptionSetValue)entity.Attributes[field]).Value.ToString();
//need to get Option Set display label based on its value. This requires getting attribute metadata
RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest
{
EntityLogicalName = entity.LogicalName,
LogicalName = field,
RetrieveAsIfPublished = true
};
RetrieveAttributeResponse attributeResponse = (RetrieveAttributeResponse)orgContext.Execute(attributeRequest);
EnumAttributeMetadata attributeMetadata = (EnumAttributeMetadata)attributeResponse.AttributeMetadata;
foreach (OptionMetadata om in attributeMetadata.OptionSet.Options)
{
if (om.Value == ((OptionSetValue)entity.Attributes[field]).Value)
{
fieldlabel = om.Label.UserLocalizedLabel.Label;
}
}
return fieldlabel;

SharePoint: Make a list field Required programmatically

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.

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";

Sharepoint webpart combobox of lists

I have a webpart that works off of a list but what I'm trying to do create a dropdown that contains a list of sharepoint lists so that when the user edits the page and selects 'modify shared webpart' they are able to choose a list item and that gets parsed back to the webpart.
Any examples or links to examples appreciated!
Thanks
Dan
What you are looking for is called a Toolpart. Take a look at this example for a tutorial on how to create one.
Overall, your general steps will be:
Create your custom Toolpart class inheriting from Microsoft.SharePoint.WebPartPages.ToolPart
In your custom Toolpart, override CreateChildControls, write the code to iterate over the lists in your SPWeb, and add those to a DropDownList
In your webpart, override GetToolParts and add your custom ToolPart so that it shows up in the right hand side
It sounds like you want to create a custom editor part. In the part you would have one dropdown that shows the names of the lists (you probably want to filter hidden and empty lists) and, when an item is selected from the list, a second dropdown shows the Title column of the items from the selected list.
Here's some code (edited here, so it will need to be cleaned up) to help you get started:
protected Page_Load(...)
{
if (IsPostBack) return;
var web = SPContext.Current.Web;
var query = from list in web.Lists
where list.Hidden == false && list.ItemCount == 0
select list;
DropDownList1.DataSource = query;
DropDownList1.DataTextField = "Title";
DropDownList1.DataBind();
}
protected DropDownList1_SelectedIndexChanged(...)
{
var web = SPContext.Current.Web;
var listName = DropDownList1.Text;
var list = web.Lists[listName];
var table = list.Items.GetDataTable();
DropDownList2.DataSource = table;
DropDownList2.DataTextField = "Title";
DropDownList2.DataValueField = "ID";
DropDownList2.DataBind();
}

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