1

I want to store a custom, hidden field/property on a document. This property should be hidden from the user in the UI, and I don't want to create a content type for this.

Using

var document = client.Web.GetFileByServerRelativeUrl("/UBs%20Library/webhooks.png");

and then

document.ListItemAllFields["_hidden_documentId"] = "abc"

doesn't work as it requires a content type. I tried document.ListItemAllFields.FieldValues but that doesn't seem to be persisted?

Is it even possible to do this?

2

You can use PropertyBag for that purposes. It can be used to store data at List Item level (and many others as well) and can be accessed from code only. Please find below how you can set and retrieve such a value:

        //Set value
        var item = <Code to obtain specific item/document>;
        item.Properties["_hidden_documentId"] = "DocumentId";
        item.Update();
        _clientContext.ExecuteQuery();

        //Get value
        var itemProperties = item.Properties;
        _clientContext.Load(itemProperties);
        _clientContext.ExecuteQuery();
        var documentId = itemProperties["_hidden_documentId"];

Update Please find below piece of code which I tested and it works just fine:

        var file = _clientContext.Web.GetFileByUrl("Shared Documents/test.txt");
        file.Properties["_hidden_documentId"] = "DocumentId";
        file.Update();
        _clientContext.ExecuteQuery();

        //Get value
        var fileProperties = file.Properties;
        _clientContext.Load(fileProperties);
        _clientContext.ExecuteQuery();
        var documentId = fileProperties["_hidden_documentId"];

Properties property is available for File as well.

  • Thanks for the reply. Using CSOM, how do I get the PropertyBag using client.Web.GetFileByServerRelativeUrl()? I.e. how to get to it from the Microsoft.SharePoint.Client.File class? Is this on the 2013 or 2016 csom? – Umair Aug 31 '17 at 15:58
  • It works exactly the same - File class have Properties as well, you just need to replace vat item =... with var file = client.Web.GetFileByServerRelativeUrl() – rafter13 Sep 1 '17 at 5:57
  • Intellisense is not giving me the PropertyBag for document, nor ListItem. I will check in the 2016 version. – Umair Sep 1 '17 at 11:30
  • I updated my answer. To clarify - I am using SharePoint Online. – rafter13 Sep 1 '17 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.