Pregunta

I'm trying to automate some menial tasks on sharepoint by creating C# code. I think the code is correct, however when running it, I get a 401 error stating that "The remote server returned an error: (401) Unauthorized."

The credentials of the user Im using in the code has full control access over the list in question, and can perform the tasks in the code through the UI. This is the first time im programming C# so any help would be greatly appreciated!

using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;


namespace ConsoleApplication3
{
class Program
{
    static void Main()
    {
        ClientContext context = new ClientContext("websiteURL");
        context.Credentials = new NetworkCredential("user", "pasword", "domain");
        List oList = context.Web.Lists.GetByTitle("TestBI");
        ListItem oListItem = oList.GetItemById(0);

        for (int i = 0; i < 3; i++)
        {
            oListItem = oList.GetItemById(i);
            oListItem.BreakRoleInheritance(true, false);
        }
        context.ExecuteQuery();
    }
}
}
¿Fue útil?

Solución

Since you're not using a domain account, so the way to create a NetworkCredential should be:

context.Credentials = new NetworkCredential("user", "pasword");
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top