1

I am trying to create a custom column in SharePoint 2013 document library using SharePoint REST API. The column is created but I am not able to change the internal name of the column while creating. Below is my code

string metaDataName="Test Column"
string siteUrl = "http://mysite.sharepoint.com/sites/testSite/";
string metaDataInternalName = metaDataName.Replace(" ", string.Empty);
JObject json = JObject.Parse("{'__metadata': { 'type': 'SP.Field' },'FieldTypeKind': 2,'InternalName': '" + metaDataInternalName + "','StaticName':'" + metaDataInternalName + "','Title': '" + metaDataName + "','Required': true }");
int contentLen = json.ToString().Length;
var handler = new HttpClientHandler();
handler.Credentials = new System.Net.NetworkCredential("user1", "pass@123", "CompanyDomain");
string rESTUrl = siteUrl + "/_api/web/Lists/getbytitle('Library 1')/fields";
var client = new HttpClient(handler);
client.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
var digest = await GetDigestAsync(siteUrl + "/_api/contextinfo", client);
client.DefaultRequestHeaders.Add("X-HTTP-Method", "POST");
client.DefaultRequestHeaders.Add("X-RequestDigest", digest);
HttpContent strContent = new StringContent(json.ToString());
strContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
strContent.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("odata", "verbose"));
strContent.Headers.ContentLength = contentLen;
var response = await client.PostAsync(rESTUrl, strContent);
string res = response.EnsureSuccessStatusCode().StatusCode.ToString();
if (res == "Created")
{
return "MetaData Created";
}
else
{
return "MetaData not created";
}

All the other properties like 'FieldTypeKind' and 'Required' are working fine but the internal and static name is not working as expected. So how can we change the column internal name while creating them..??

Thanks in advance

  • Can you try Name instead of InternalName and see if it works. – Nadeem Yousuf May 29 '18 at 7:54
  • @NadeemYousuf : No it is not working. Infact by changing from Internal Name to Name the server returns the Bad Request error – Ankit Katiyar May 29 '18 at 9:48
3

InternalName is a readonly property so having it while creating a field may not work. Set your field properties using SchemaXml property. See this for more information: https://msdn.microsoft.com/en-us/library/office/dn600182.aspx

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.