locked
SharePoint`s REST API in external systems RRS feed

  • Question

  • Hi!

    Is it possible to retrieve list items and create an item in SharePoint list via SP`s REST API from external system?

    How in this case authorization takes place? Is giving Contribute permissions in the list for the system account of the sending REST requests external system enough?

    Is it possible to create a HTTP authorization for the SP REST API as alternative?

    Thank you in advance!


    • Edited by Ashina369 Wednesday, April 17, 2019 1:29 PM
    Wednesday, April 17, 2019 1:28 PM

Answers

  • Hi,

    Here is sample code for your reference.

    private static async Task<string>getWebTitle(string webUrl) 
            { 
                //Creating Password 
                const string PWD = "softjam.1"; 
                const string USER = "bubu@zsis376.onmicrosoft.com"; 
                const string RESTURL = "{0}/_api/web?$select=Title"; 
     
                //Creating Credentials 
                var passWord = new SecureString(); 
                foreach (var c in PWD) passWord.AppendChar(c); 
                var credential = new SharePointOnlineCredentials(USER, passWord); 
     
                //Creating Handler to allows the client to use credentials and cookie 
                using (var handler = new HttpClientHandler() { Credentials = credential }) 
                { 
                    //Getting authentication cookies 
                    Uri uri = new Uri(webUrl); 
                    handler.CookieContainer.SetCookies(uri, credential.GetAuthenticationCookie(uri)); 
     
                    //Invoking REST API 
                    using (var client = new HttpClient(handler)) 
                    { 
                        client.DefaultRequestHeaders.Accept.Clear(); 
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
     
                        HttpResponseMessage response = await client.GetAsync(string.Format(RESTURL, webUrl)).ConfigureAwait(false); 
                        response.EnsureSuccessStatusCode(); 
     
                        string jsonData = await response.Content.ReadAsStringAsync(); 
     
                        return jsonData; 
                    } 
                } 
            }

    https://code.msdn.microsoft.com/office/Invoke-SharePoint-REST-API-078a0638

    While you could use CSOM api directly.

    https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code

    SharePoint CSOM sdk.

    https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Proposed as answer by Dennis Guo Monday, April 29, 2019 9:33 AM
    • Marked as answer by Ashina369 Tuesday, April 30, 2019 3:03 AM
    Thursday, April 18, 2019 1:43 AM

All replies

  • you can try the following. https://docs.microsoft.com/en-us/sharepoint/dev/general-development/how-to-access-external-data-with-rest-in-sharepoint

    Please remember to click Mark as Answer on the answer if it helps you

    • Marked as answer by Ashina369 Tuesday, April 30, 2019 3:03 AM
    • Unmarked as answer by Ashina369 Tuesday, April 30, 2019 3:03 AM
    Wednesday, April 17, 2019 5:23 PM
  • Hi,

    Here is sample code for your reference.

    private static async Task<string>getWebTitle(string webUrl) 
            { 
                //Creating Password 
                const string PWD = "softjam.1"; 
                const string USER = "bubu@zsis376.onmicrosoft.com"; 
                const string RESTURL = "{0}/_api/web?$select=Title"; 
     
                //Creating Credentials 
                var passWord = new SecureString(); 
                foreach (var c in PWD) passWord.AppendChar(c); 
                var credential = new SharePointOnlineCredentials(USER, passWord); 
     
                //Creating Handler to allows the client to use credentials and cookie 
                using (var handler = new HttpClientHandler() { Credentials = credential }) 
                { 
                    //Getting authentication cookies 
                    Uri uri = new Uri(webUrl); 
                    handler.CookieContainer.SetCookies(uri, credential.GetAuthenticationCookie(uri)); 
     
                    //Invoking REST API 
                    using (var client = new HttpClient(handler)) 
                    { 
                        client.DefaultRequestHeaders.Accept.Clear(); 
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
     
                        HttpResponseMessage response = await client.GetAsync(string.Format(RESTURL, webUrl)).ConfigureAwait(false); 
                        response.EnsureSuccessStatusCode(); 
     
                        string jsonData = await response.Content.ReadAsStringAsync(); 
     
                        return jsonData; 
                    } 
                } 
            }

    https://code.msdn.microsoft.com/office/Invoke-SharePoint-REST-API-078a0638

    While you could use CSOM api directly.

    https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code

    SharePoint CSOM sdk.

    https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Proposed as answer by Dennis Guo Monday, April 29, 2019 9:33 AM
    • Marked as answer by Ashina369 Tuesday, April 30, 2019 3:03 AM
    Thursday, April 18, 2019 1:43 AM
  • Hi

    Thank you, but it is notthe case

    Tuesday, April 30, 2019 3:04 AM
  • Hi

    Thank you for your detailed answer, Lee!

    Tuesday, April 30, 2019 3:04 AM