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.