1

Currently I'm started learning CSOM (C#). I have created web part in Farm Solution. and deploy on local site. Web part is use for display Web title.

It shows error on line context.ExecuteQuery();

Access denied. You do not have permission to perform this action or access this resource.

enter image description here

I'm using this MSDN article

Please suggest some answers

2

Try to set credential to your context as the following

using (ClientContext context = new ClientContext("http://PC5/")) {
    context.Credentials = new NetworkCredential("user", "password", "domain");
    //your code 
    context.ExecuteQuery();
}
  • M.Qassas. It's working for me. may be I'm wrong, Is it bad practice to pass credentials from web part? – Akshay Dattatray Nangare Oct 19 '16 at 9:27
  • Is that reason for not to use CSOM in Web Part. because I had mostly work on server object – Akshay Dattatray Nangare Oct 19 '16 at 9:30
  • Why it's bad?!, you need to define the context credential, to avoid access denied! however, the best practice is to define this credential as key in web config and read it as mentioned at codenotation.com/2011/12/… – Mohamed El-Qassas MVP Oct 19 '16 at 9:35
1

You need to use it like this:

string siteUrl = args.Length < 2 ? "https://tenant.sharepoint.com/sites/customsite/" : args[0];
            string userName = args.Length < 2 ? "abc@tenant.onmicrosoft.com" : args[1];
            string password = args.Length < 2 ? "password" : args[2];



            using (ClientContext context = new ClientContext(siteUrl))
            {
                SecureString securePassword = new SecureString();
                foreach (char c in password.ToCharArray())
                {
                    securePassword.AppendChar(c);
                }

                context.AuthenticationMode = ClientAuthenticationMode.Default;
                context.Credentials = new SharePointOnlineCredentials(userName, securePassword);

                Web oWeb = context.Web;
                context.Load(oWeb, website => website.Title);
                context.ExecuteQuery();
            }
  • I'm not using sandbox solution , its simply Farm solution. so your code still work for Web part on local farm – Akshay Dattatray Nangare Oct 19 '16 at 9:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.