I am working with document sets in SharePoint Online. Is there a way with Powershell or C# I can set the values of the document set fields?
Any examples?
It's not clear from what I have seen on the internet.
Thanks
I am working with document sets in SharePoint Online. Is there a way with Powershell or C# I can set the values of the document set fields?
Any examples?
It's not clear from what I have seen on the internet.
Thanks
Assuming you have the document set object $docSet
, the metadata of the document set can be updated as below.
$docSet.Folder.Properties["MetadataProp1"]="test"
$docSet.Folder.Update()
$docLib.Update()
$web.Update()
The following C# code for your reference.
string targetSiteURL = @"https://xx.sharepoint.com/sites/lz";
var login = "lz@xx.onmicrosoft.com";
var password = "**";
string documentLibraryName = "DocumentLibrary";
string documentSetName = "DdocumentSet1";
string propertyName = "field1";
string updatePropertyValue = "LZ";
var securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
ClientContext ctx = new ClientContext(targetSiteURL);
ctx.Credentials = onlineCredentials;
Web site = ctx.Web;
List docLib = site.Lists.GetByTitle(documentLibraryName);
ctx.Load(docLib);
CamlQuery caml = new CamlQuery();
caml.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='text'>"+documentSetName+"</Value></Eq></Where></Query></View>"; ;
ListItemCollection items = docLib.GetItems(caml);
ctx.Load(items);
ctx.ExecuteQuery();
if (items.Count != 0)
{
ListItem item = items[0];
var properties = item.Folder.Properties;
properties[propertyName] = updatePropertyValue;
item.Folder.Update();
ctx.ExecuteQuery();
}