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 Answers 2

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();
}
3
  • M.Qassas. It's working for me. may be I'm wrong, Is it bad practice to pass credentials from web part? Oct 19, 2016 at 9:27
  • Is that reason for not to use CSOM in Web Part. because I had mostly work on server object Oct 19, 2016 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/… Oct 19, 2016 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 ? "[email protected]" : 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();
            }
1
  • I'm not using sandbox solution , its simply Farm solution. so your code still work for Web part on local farm Oct 19, 2016 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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