none
'Microsoft.SharePoint.Client.ServerUnauthorizedAccessException' occurred in Microsoft.SharePoint.Client.Runtime.dll in SP2013 RRS feed

  • Question

  • Hi All,

    I have written a client side program to access the list items from sharepoint 2013  using CSOM. When i execute  clientContext.ExecuteQuery(); i'm getting the following error.

    'Microsoft.SharePoint.Client.ServerUnauthorizedAccessException' occurred in Microsoft.SharePoint.Client.Runtime.dll

    I have a question here

    Is it still possible to write a simple CSOM program to access sharepoint 2013 site. or do we have to create a provider hosted app to access the sharepoint 2013 site.

    Friday, March 31, 2017 1:22 PM

All replies

  • Hi,

    Please check the following code to get list items using CSOM.

    using System;
    using Microsoft.SharePoint.Client;
    using SP = Microsoft.SharePoint.Client;
    
    namespace Microsoft.SDK.SharePointServices.Samples
    {
        class RetrieveListItems
        {
            static void Main()
            {
                string siteUrl = "http://MyServer/sites/MySiteCollection";
    
                ClientContext ctx = new ClientContext(siteUrl);
    			ctx.Credentials = new System.Net.NetworkCredential("userName", "password", "domain");
                SP.List oList = ctx.Web.Lists.GetByTitle("customlist");
    
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = "<View><RowLimit>100</RowLimit></View>";
    			
                ListItemCollection collListItem = oList.GetItems(camlQuery);
    
                ctx.Load(collListItem);
    
                ctx.ExecuteQuery();
    
                foreach (ListItem oListItem in collListItem)
                {
                    Console.WriteLine("ID: {0} \nTitle: {1}", oListItem.Id, oListItem["Title"]);
                }
            }
        }
    }
    

    Or I suggest you provide the complete code in use if possible, which should make the issue easier to be researched.

    About provider-hosted add-in, please check the article below:

    Get started creating provider-hosted SharePoint Add-ins

    https://msdn.microsoft.com/en-us/library/office/fp142381.aspx


    Best Regards,

    Dennis


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

    Monday, April 3, 2017 8:09 AM
    Moderator
  • For Accessing the SP2013 Online side using CSOM server side code, we need to pass the credential of user:

    public void GetListData(string _siteURL, string _listName) 
    {
    
    try
    {
     SecureString password = GetSecurePassword();
    using (var ctx = new ClientContext(_siteURL))
    {
    
                        ctx.AuthenticationMode = ClientAuthenticationMode.Default;
                        ctx.Credentials = new SharePointOnlineCredentials("XXXXX", password);
    
                        //Get the Site Collection
                        Site oSite = ctx.Site;
                        ctx.Load(oSite);
                        ctx.ExecuteQuery();
    
                        // Get the Web
                        Web oWeb = ctx.Web;
                        ctx.Load(oWeb);
                        ctx.ExecuteQuery();
    
                        CamlQuery query = new CamlQuery();
                        query.ViewXml = @"";
    
                        Microsoft.SharePoint.Client.List oList = ctx.Web.Lists.GetByTitle(_listName);
                        ctx.Load(oList);
                        ctx.ExecuteQuery();
    
                        
                        ListItemCollection items = oList.GetItems(query);
                        ctx.Load(oList.Fields);
                        ctx.Load(oList.Views);
                        ctx.Load(items);
                        ctx.ExecuteQuery();
    
    foreach (Microsoft.SharePoint.Client.ListItem listItem in items)
    {
       //To Do...
    }
    
    
    
    }
    }
    catch (Exception ex)
    {
    }
    
    }
    
    
           public SecureString GetSecurePassword()
           {
                          SecureString pwd = null;
               try
               {
                   string password = "XXXXX";
                   
                   pwd = new SecureString();
                   foreach (char x in password)
                   {
                       pwd.AppendChar(x);
                   }
               }
               catch (Exception ex)
               {
                              }
               finally
               {
                              }
               return pwd;
           }


    Thanks, Amit Kumar, LinkedIn Profile ** My Blog



    Friday, April 7, 2017 7:41 AM
  • Hi Sathees

    Can you please ensure that you are running the program as a user who has at least read permissions to the list items?

    A simple check will be to log in as the user you run the program with to the SharePoint site and check whether the list items details are displayed. 

    Regards

    Reshmee

    Friday, April 7, 2017 1:29 PM