1

Iam developing a Custom MenuItemAction action so that each listitem in the Copy list of site collection (dev) will get an option for copying the documents to another list (Paste) in site collection(dev1) using provider hosted app for SharePoint online using VS2015. I am trying to save file with filePath from Copy List. I had give Full Control at tenant level for the App.But i am getting below Access denied. You do not have permission to perform this action or access this resource at destinationContext.ExecuteQuery();

Below is my code. I haven't change any code in the SharePointContext.cs and TokenHelper.cs

I know this is an authentication problem can anyone help me on how to implement the authentication for accessing the another site collection(dev1).

        string filePath = "/sites/dev/Copy/TypeScript_Succinctly.pdf";
            var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
            using (var sourceContext = spContext.CreateUserClientContextForSPHost())
            {
                Microsoft.SharePoint.Client.File sourceFile = sourceContext.Web.GetFileByServerRelativeUrl(filePath);
                //file stream will be used to upload in destination library
                var fileStream = sourceFile.OpenBinaryStream();
                sourceContext.Load(sourceFile, k => k.ServerRelativeUrl, k => k.Name);
                sourceContext.ExecuteQuery();
                //generate destination context
                string destinationSiteUrl = "https://tarun.sharepoint.com/sites/dev1";
                Uri destinationSiteUri = new Uri(destinationSiteUrl);
                //target realm of the tenant (This is constant per tenant)
                string targetRealm = TokenHelper.GetRealmFromTargetUrl(destinationSiteUri);
                //generate access token for destination site
                string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, destinationSiteUri.Authority, targetRealm).AccessToken;
                //get destination site context using access token
                using (var destinationContext = TokenHelper.GetClientContextWithAccessToken(destinationSiteUrl, accessToken))
                {
                    //load server relative url of destination library
                    var destinationLibrary = destinationContext.Web.Lists.GetByTitle("Paste");
                    FileCreationInformation fileCreationInfo = new FileCreationInformation();
                    fileCreationInfo.ContentStream = fileStream.Value;
                    fileCreationInfo.Url = sourceFile.Name;
                    fileCreationInfo.Overwrite = false;
                    Microsoft.SharePoint.Client.File destinationFile = destinationLibrary.RootFolder.Files.Add(fileCreationInfo);
                    destinationContext.Load(destinationFile, k => k.ServerRelativeUrl);
                    destinationContext.ExecuteQuery();
                    lbl_Success.Text = "Copied Sucessfully";
                }
            }

Can Anyone help me please.Thanks in Advance.

1

I see that you are using the App-only policy.

Ensure that in the Appmanifest.xml, you have given write permission to the tenant. Check below screenshot.

enter image description here

  • @ Gautam Sheth, as mentioned in the question i had given Full Control at tenanat level in the app manifest. – TARUN Feb 13 '17 at 14:20
  • umm is the Allow the app to make app-only calls checked ? – Gautam Sheth Feb 13 '17 at 14:22
  • Check the screenshot again , there is a checkbox which is checked. – Gautam Sheth Feb 13 '17 at 14:32

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.