3

I am using SharePoint.Client to connect to a SharePoint website. The first time it works, but after that I get 401 : Unauthorized error.

        using (ClientContext context = new ClientContext(siteUrl))
        {
            Web web = context.Web;

            List announcementsList = context.Web.Lists.GetByTitle(listName);

            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
            ListItem newItem = announcementsList.AddItem(itemCreateInfo);
            newItem["Title"] = "My New Item!";
            newItem["MyField2"] = 36;
            newItem.Update();

            context.ExecuteQuery();
        }

I have tried entering my credentials also.

context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = new NetworkCredential("username", "password", "domain");

But the error still remains after a request. I have also encountered that after a time I can make again request, then randomly get Unauthorized. Is there a timeout on SharePoint server or a restriction on multiple requests?

  • 1
    There is a timeout for the ClientContext object, but you should get an error stating so, not a 401. You should also be able to make multiple requests. Do you have permission to the site the context is created for? – wjervis Feb 16 '16 at 15:23
  • If you make a lot of consecutive request (like couple hundreds) SharePoint can deny the service. It did happen to me a couple of times, but I think error code was 429 "Too many requests". – dstarkowski Feb 16 '16 at 15:39
  • I have all the permissions and on the web interface I can do all the operations I want to to from code. Besides that I do manage to get randomly the operation through without an error. @dstarkowski Yes, I thought that could be the problem so I created a button that does calls this method, same results, with error code 401 Unauthorized. – dare2k Feb 17 '16 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.