Answered by:
Exception

Question
-
Hi,
What does REST API throw as an exception when trying to write a duplicate field to a field that enforces uniqueness constraint?
Please help
techie
Tuesday, March 24, 2015 9:30 PM
Answers
-
Depends what API you are using. If you are using HttpWebRequest you will receive an "Internal Server Error (500)" error. If you are using the HttpClient you will receive an "Bad Request (400)". If you are using ajax and javascript then you will get a more informed message.
Blog | SharePoint Field Notes Dev Tools | SPFastDeploy | SPRemoteAPIExplorer
- Marked as answer by Eric Tao Friday, April 3, 2015 1:44 AM
Wednesday, March 25, 2015 5:20 PM -
You can catch the true exception using the HttpClient class. Use response.Content.ReadAsStringAsync(). This will return json, with this error
"-2130575169, Microsoft.SharePoint.SPDuplicateValuesFoundException"
Code:
string url = "http://basesmc15/"; HttpClient client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }); client.BaseAddress = new System.Uri(url); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose"); client.DefaultRequestHeaders.Add("X-RequestDigest", GetFormDigest("http://basesmc15", CredentialCache.DefaultNetworkCredentials, null)); client.DefaultRequestHeaders.Add("X-HTTP-Method", "MERGE"); client.DefaultRequestHeaders.Add("IF-MATCH", "*"); string json = "{'__metadata': { 'type': 'SP.Data.TestlistListItem' }, 'lookerId': 5}"; client.DefaultRequestHeaders.Add("ContentLength", json.Length.ToString()); HttpResponseMessage response = null; try { StringContent strContent = new StringContent(json); strContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose"); response = client.PostAsync("_api/web/lists/getbytitle('testlist')/items(2)", strContent).Result; var content = response.Content.ReadAsStringAsync(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); }
Blog | SharePoint Field Notes Dev Tools | SPFastDeploy | SPRemoteAPIExplorer
Wednesday, March 25, 2015 6:43 PM
All replies
-
Hi,
Would you please provide the error message and which REST API url you are using ? It will be easier to find the root cause of the issue.
Thanks for your understanding.
Best Regards
TechNet Community Support
Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.Wednesday, March 25, 2015 8:50 AM -
The issue is :
When we use REST API to POST a value on a field that has uniqueness constraint enforced, we don't receive any error.
Is this by design? or what error is thrown?
techie
Wednesday, March 25, 2015 5:17 PM -
Depends what API you are using. If you are using HttpWebRequest you will receive an "Internal Server Error (500)" error. If you are using the HttpClient you will receive an "Bad Request (400)". If you are using ajax and javascript then you will get a more informed message.
Blog | SharePoint Field Notes Dev Tools | SPFastDeploy | SPRemoteAPIExplorer
- Marked as answer by Eric Tao Friday, April 3, 2015 1:44 AM
Wednesday, March 25, 2015 5:20 PM -
You can catch the true exception using the HttpClient class. Use response.Content.ReadAsStringAsync(). This will return json, with this error
"-2130575169, Microsoft.SharePoint.SPDuplicateValuesFoundException"
Code:
string url = "http://basesmc15/"; HttpClient client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }); client.BaseAddress = new System.Uri(url); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose"); client.DefaultRequestHeaders.Add("X-RequestDigest", GetFormDigest("http://basesmc15", CredentialCache.DefaultNetworkCredentials, null)); client.DefaultRequestHeaders.Add("X-HTTP-Method", "MERGE"); client.DefaultRequestHeaders.Add("IF-MATCH", "*"); string json = "{'__metadata': { 'type': 'SP.Data.TestlistListItem' }, 'lookerId': 5}"; client.DefaultRequestHeaders.Add("ContentLength", json.Length.ToString()); HttpResponseMessage response = null; try { StringContent strContent = new StringContent(json); strContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose"); response = client.PostAsync("_api/web/lists/getbytitle('testlist')/items(2)", strContent).Result; var content = response.Content.ReadAsStringAsync(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); }
Blog | SharePoint Field Notes Dev Tools | SPFastDeploy | SPRemoteAPIExplorer
Wednesday, March 25, 2015 6:43 PM