4

I am having a requirement where I need to update the Managed Metadata column value using REST API or SharePoint Web Service.

I have gone through this and this and this post but this won't help me because I am not having Term Guid.

What I have is only Term Label. So I want to know is there REST / SharePoint Web Service example using which I can set value in my Managed Metadata column value?

Again! I am not having SharePoint Environment to work, so CSOM, JSOM and SSOM will won't work for me.


Update : I somehow managed to get TermGuid by TermName using SharePoint Taxonomy Web Service. Now I am having TermGuid, Label and I tried but still I am not able to update the field value.

Here is my code which I am using to update the Managed Metadata field.

var digest = GetFormDigest();
if (!string.IsNullOrEmpty(digest))
{
    var url = siteUrl + "_api/Web/GetFileByServerRelativeUrl('/" + libraryName + "/" + fileName + "')/ListItemAllFields";
    var body = "{ '__metadata' : {'type': 'SP.Data.ListNameListItem},'FieldName' : { '__metadata' : {'type': 'SP.Taxonomy.TaxonomyFieldValue'},'Label': 'TermLabel','TermGuid':'TermGuid','WssId': -1 } } ";
    var client = new WebClient();
    client.Credentials = creds;
    client.Headers[HttpRequestHeader.Accept] = "application/json;odata=verbose";
    client.Headers[HttpRequestHeader.ContentType] = "application/json;odata=verbose";
    client.Headers["X-RequestDigest"] = digest;
    client.Headers["IF-MATCH"] = "*";
    client.Headers["X-Http-Method"] = "MERGE";
    var json = client.UploadString(url, body);
}

I am following this post now. I crossed checked for internal name, my internal name is 100% correct for MMD field


Update

I again somehow managed the code with SharePoint List Web Service Method from this post. The code I used is as follow:

Lists list = new Lists();
list.Credentials = creds;
XmlNode ndListView = list.GetListAndView(libraryName, "");
string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value;
XmlDocument docNew = new XmlDocument();
XmlElement batchElement = docNew.CreateElement("Batch");
batchElement.SetAttribute("OnError", "Return");
batchElement.SetAttribute("ListVersion", "1");
batchElement.SetAttribute("ViewName", strViewID);
batchElement.InnerXml = "<Method ID='1' Cmd='Update'>" +
"<Field Name='ID'>2</Field>" +
"<Field Name='Title'>Modified through lists.asmx</Field>" +
"<Field Name='City'>" + termValue + "</Field></Method>";
XmlNode ndResult;
try
{
       ndResult = list.UpdateListItems(strListID, batchElement);
}
catch (Exception soapException)
{
}

Where City is my MMD fields internal Name and termValue : "-1;#TermName|TermGuid"

Still the Non Managed Metadata fields got updated but MMD field does not. Any kind of help and suggestion are appreciated.

  • I can see you are calling the REST API from c# code.I would say that you can very well use c# CSOM here to avoid the hassle of using Taxonomy web service – Unnie Jul 16 '16 at 9:51
  • I can't go with CSOM as I am not having SharePoint Environment to run this code. Sorry I forgot to mention that. I am having only 2 options, REST from c# or SharePoint Web Service – Aakash Maurya Jul 16 '16 at 9:53
  • For CSOM you don't need access to SharePoint server. You can run the c# code remotely outside the server for e.g. any other web server or your PC. The only thing you require is access to SharePoint site. – Unnie Jul 16 '16 at 10:14
  • Thanks. I am just bound to Rest and SharePoint Web Service. I will discuss with my higher authority regarding CSOM once again. But we have already achieved this task using SharePoint Web Service but unfortunately we don't have any reference of that task. – Aakash Maurya Jul 16 '16 at 10:18
4

The problem for you is that you only have Term Label and Term Guid is not available. There is no REST API support for term store yet, so you cannot query the termset and get the guid for the Term Label.

If you are looking for C# CSOM code to update taxonomy field value see this Post

  • What about SharePoint Web Service? – Aakash Maurya Jul 16 '16 at 2:06
  • Missed that part. Glad that you figured it out – Unnie Jul 16 '16 at 9:46

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.