DocumentSet.Import returns [Exception : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))]
-
22-01-2021 - |
Question
I'm getting the [Exception: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))]
when trying to import documentSet programmatically from a site collection document library to another document lib located in on of his subsites.
PS: this code works perfectly with a FULL Control in site collection but not with contribute or Modification Role.
My question is what is the minimum role to be able to execute the code?
SPList publishingList = publishingWeb.TryGetListByUrl("PublishedCases/");
byte[] compressedFile = docSet.Export();
SPContentType secondCt = publishingList.ContentTypes["DOCLIB"];
SPFolder targetFolder = publishingList.RootFolder;
DocumentSet publishedDocSet = DocumentSet.Import(compressedFile, myitem.Name, targetFolder, secondCt.Id, properties, elevateWeb.CurrentUser);
Regards,
Solution
I actually did it with Elevate Privileges but still dont know the minimum permissions to be able to create document Set programatically.
SPUserToken sysToken = SPContext.Current.Site.SystemAccount.UserToken;
using (SPSite elevatedSite = new SPSite(site.ID, sysToken))
{
using (SPWeb elevatePublishingWeb = elevatedSite.OpenWeb("Publishing"))
{
//Code here ....
SPList publishingList =
elevatePublishingWeb.TryGetListByUrl("PublishedCases/");
byte[] compressedFile = docSet.Export();
SPContentType secondCt = publishingList.ContentTypes["CT_DOCLIB"];
SPFolder targetFolder = publishingList.RootFolder;
DocumentSet publishedDocSet = DocumentSet.Import(compressedFile,
myitem.Name, targetFolder, secondCt.Id, properties, elevateWeb.CurrentUser);
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange