How to set the values of Metadata (column fields) of Sharepoint Documents

Author - Umang Pasricha
11.09.2019
|
0 Comments
||

How to set the values of Metadata(Column fields) of Sharepoint Documents

Here is the code in C# which helps you to change the metadata field values of Sharepoint Files.

//This manager class can be used to obtain a SharePointContext object
OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(_siteUrl, _userId, _password));
//Load the Document List of sharepoint
Web web = ctx.Web;
List list = web.Lists.GetByTitle(“Documents”);
ctx.Load(list);
ctx.ExecuteQueryRetry();
//Returns the file object located at the specified server-relative URL(Path to a file)
Microsoft.SharePoint.Client.File newFile = web.GetFileByServerRelativeUrl(web.ServerRelativeUrl + “/Shared%20Documents/FolderName/fileName”);
//Get all the fields of the File
ListItem item = newFile.ListItemAllFields;
newFile.CheckOut();
//Update the metadata with field names (Source_x0020_Object & Source_x0020_Record_x0020_ID)
item[“Source_x0020_Object”] = value1;
item[“Source_x0020_Record_x0020_ID”] = value2;
item.Update();
//Checking in the file will update the Modified field to the date/time at which the file was checked in
newFile.CheckIn(“Date Created Updated”, CheckinType.MajorCheckIn);
ctx.ExecuteQuery();
ctx.Load(item);
ctx.ExecuteQuery();
int id = item.Id;

Main Problem that people face during this functionality:
To change the metadata field values, we can’t use their name that we give to the columns while creating custom columns. In above example I have used Source_x0020_Object and Source_x0020_Record_x0020_ID column names. You can find these names of your columns by clicking on your column from Document Library Settings. You will be moved to Edit Column Page. On this page, we will see this name in the URL against the attribute “Field”. Copy this name and use it to change column value.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *