H P
H P

Reputation: 79

I am trying to access sharepoint via C# code and i am getting UNAUTHORIZED access error but I can log web sharepoint without any issues

I am working on C#.net application that will interact with Sharepoint 2013 using the client object model approach to download and upload files. When I use my credentials to run the C#.net application it gives me below error.

But if I use same credentials to log in via Web Sharepoint it works fine. I don't understand what I need to plug into my code in order to access SharePoint to download/upload files.

Any suggestions will be helpful. Thanks in advance!

   public void test()
    {
        Console.WriteLine("SharePoint Online site URL:");

        try
        {
            using (var context = new ClientContext(webSPOUrl))
            {
                Web web = context.Web;
                context.Load(web, website => website.Title);
                context.Load(web.Webs);

                CredentialCache cc = new System.Net.CredentialCache();
                cc.Add(new Uri(webSPOUrl), "NTLM", CredentialCache.DefaultNetworkCredentials);
                context.Credentials = cc;
                context.AuthenticationMode = ClientAuthenticationMode.Default;

                context.Load(web.Lists,
                    lists => lists.Include(list => list.Title,
                        list => list.Id));


                context.ExecuteQuery();
                Console.ForegroundColor = ConsoleColor.White;
                foreach (List list in web.Lists)
                {
                    Console.WriteLine("List title is: " + list.Title);
                }
                Console.WriteLine("");

            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error is: " + ex.Message);
        }
    }

Upvotes: 0

Views: 2387

Answers (4)

H P
H P

Reputation: 79

Thank you, everyone, who have provided the solutions. Below is the working code:

public static void Uploadfiles(string SiteUrl, string DocLibrary, string ClientSubFolder, string FileName)
        {

            string webSPOUrl = "https://testdev.sharepoint.com/sites/test";
            Console.WriteLine("User Name:");
            string userName = "<your username >@testdev.onmicrosoft.com";
            Console.WriteLine("Password:");
            string librayName = "Documents";


                try
                {
                    using (var context = new SP.ClientContext(webSPOUrl))
                    {
                        // Sharepoint Online Authentication
                        context.Credentials = new SP.SharePointOnlineCredentials(userName, password);
                        SP.Web web = context.Web;

                        SP.FileCreationInformation newFile = new SP.FileCreationInformation();
                        byte[] FileContent = System.IO.File.ReadAllBytes(FileName);
                        newFile.ContentStream = new System.IO.MemoryStream(FileContent);
                        newFile.Url = Path.GetFileName(FileName);
                        SP.List DocumentLibrary = web.Lists.GetByTitle(DocLibrary);

                        SP.Folder Clientfolder = DocumentLibrary.RootFolder.Folders.Add(ClientSubFolder);

                        Clientfolder.Update();
                        SP.File uploadFile = Clientfolder.Files.Add(newFile);

                        context.Load(DocumentLibrary);
                        context.Load(uploadFile);
                        context.ExecuteQuery();
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("The File has been uploaded" + Environment.NewLine + "FileUrl -->" + SiteUrl + "/" + DocLibrary + "/" + ClientSubFolder + "/" + Path.GetFileName(FileName));
                     } }

Upvotes: 1

LZ_MSFT
LZ_MSFT

Reputation: 4228

If you want to access SharePoint 2013 on-premise in client side, you can modify the code as below.

public void test()
{
    try
    {
        using (var context = new ClientContext(webSPOUrl))
        {
            context.Credentials = new NetworkCredential("username", "password", "domain");
            Web web = context.Web;
            context.Load(web, website => website.Title);
            context.Load(web.Webs);
            context.Load(web.Lists,
                lists => lists.Include(list => list.Title,
                    list => list.Id));

            context.ExecuteQuery();
            Console.ForegroundColor = ConsoleColor.White;
            foreach (List list in web.Lists)
            {
                Console.WriteLine("List title is: " + list.Title);
            }
            Console.WriteLine("");

        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error is: " + ex.Message);
    }
}

Upvotes: 0

Paul-Jan
Paul-Jan

Reputation: 17278

From the code snippet you provided, it seems you are trying to connect to SharePoint Online (O365). The network credentials object you use in your code is for connecting to traditional 'on premise' SharePoint installs. You should be using SharePointOnlineCredentials instead.

context.Credentials = new SharePointOnlineCredentials(userName,password);
context.Load(...);
context.ExecuteQuery();

Upvotes: 0

EylM
EylM

Reputation: 6103

If you could provide a user name, password and domain, this code worked for me:

var clientContext = new ClientContext(new Uri(someUrl))
{
    Credentials = new System.Net.NetworkCredential("UserName","Password", "DomainName")
}

Upvotes: 0

Related Questions