0

We have a event receiver that runs some custom code to change permissions on the ItemAdded event. The permission changes are made inside of an elevated scope using an elevated web.

Both before and after the permissions change, the creator of this DocumentSet has permissions to contribute, but sometimes the creator isn't redirected to the DocSet when created, but instead is shown a Access Denied error message. At other times it shows a NullReferenceException error message, though none of the errors come from the custom code. Also, the custom code always runs successfully and the permissions have been set correctly, even if the error shows up.

Here's part of the code, though I omitted the parts for the permission change as they are the usual SP permission change code.

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using Microsoft.Office.DocumentManagement.DocumentSets;
using Microsoft.SharePoint.Administration;
using CustomCode.Services;
using CustomCode.Extensions;
using System.Collections.Generic;

namespace CustomCode.DocumentSetSubmittedReceiver
{
    public class DocumentSetSubmittedReceiver : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            var web = properties.Web;
            var listItem = properties.ListItem;

            if(listItem == null)
            {
                return;
            }

            if (properties.ListTitle != "Documents" ||
                listItem.ContentType.Name != "Custom content type")
            {
                return;
            }

            //Elevate uses  SPSecurity.RunWithElevatedPrivileges and web.Site.SystemAccount.UserToken to get elevate the web
            web.Elevate((elevatedWeb) =>
            {
                var documentList = elevatedWeb.Lists[listItem.ParentList.ID];
                var listItemElevated = documentList.GetItemById(listItem.ID);
                //Break Inheritance code
                //Remove all existing permissions
                //Add new calculated permissions
            });
        }
    }
}

This is the access denied error that I'm getting in the logs:

<nativehr>0x80070005</nativehr><nativestack></nativestack>Access denied.    18547c9e-93f6-d0b0-b463-a5a3d77c2eb1
SPRequest.GetFileAndFolderProperties: UserPrincipalName=i:0).w|s-1-5-21-842925246-651377827-1417001333-14332, AppPrincipalName= ,bstrUrl=http://confidential ,bstrStartUrl=Documents/;asdfjsaf;lsafl ,ListDocsFlags=24584 ,bThrowException=False    18547c9e-93f6-d0b0-b463-a5a3d77c2eb1
System.UnauthorizedAccessException: <nativehr>0x80070005</nativehr><nativestack></nativestack>Access denied., StackTrace:    at Microsoft.SharePoint.SPWeb.GetFileOrFolderProperties(String strUrl, ListDocsFlags listDocsFlags, Boolean throwException, SPBasePermissions& permMask)     at Microsoft.SharePoint.SPFolder.PropertiesCore(Boolean throwException)     at Microsoft.SharePoint.SPFolder.set_UniqueContentTypeOrder(IList`1 value)     at Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.ProvisionAllowedContentTypes()     at Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.Provision(Boolean bProvisionDefaultContent, SPLongOperationState longOperationState)     at Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.Provision(Boolean provisionDefaultContent, SPLo... 18547c9e-93f6-d0b0-b463-a5a3d77c2eb1
...ngOperationState longOperationState, SPListItem item)     at Microsoft.Office.DocumentManagement.Pages.NewDocSet.<>c__DisplayClass3.<>c__DisplayClass5.<OKBtn_Click>b__1(SPLongOperationState longOperationState)     at Microsoft.SharePoint.SPStatefulLongOperation.SetThreadCultureAndRun(RunStatefulOperation runOperation, SPLongOperationState state, CultureInfo culture, CultureInfo uICulture)     at Microsoft.SharePoint.SPStatefulLongOperation.<>c__DisplayClass6.<Run>b__1(Object state)     at Microsoft.SharePoint.SPSecurity.RunAsUser(SPUserToken userToken, Boolean bResetContext, WaitCallback code, Object param)     at Microsoft.SharePoint.Utilities.SPThreadPool.WaitCallbackWrapper(Object state)     at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[]...  18547c9e-93f6-d0b0-b463-a5a3d77c2eb1
... args, Object server, Object[]& outArgs)     at System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(IMessage msg, IMessageSink replySink)     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)     at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()     at System.Threading.ThreadPoolWorkQueue.Dispatch()   18547c9e-93f6-d0b0-b463-a5a3d77c2eb1
DocumentSet ProvisionAllowedContentTypes for list Documents (f9e1220d-b5e9-4c93-9641-bf3e03e18dd5): throws exception: <nativehr>0x80070005</nativehr><nativestack></nativestack>Access denied.. Stacktrace:    at Microsoft.SharePoint.SPGlobal.HandleUnauthorizedAccessException(UnauthorizedAccessException ex)     at Microsoft.SharePoint.Library.SPRequest.GetFileAndFolderProperties(String bstrUrl, String bstrStartUrl, ListDocsFlags ListDocsFlags, Boolean bThrowException, Int32& phrStatus, Object& pvarFiles, Object& pvarDirs, UInt32& pdwNumberOfFiles, UInt32& pdwNumberOfDirs)     at Microsoft.SharePoint.SPWeb.GetFileOrFolderProperties(String strUrl, ListDocsFlags listDocsFlags, Boolean throwException, SPBasePermissions& permMask)     at Microsoft.SharePoint.SPFolder.PropertiesCore(Boolean throwEx... 18547c9e-93f6-d0b0-b463-a5a3d77c2eb1
...ception)     at Microsoft.SharePoint.SPFolder.set_UniqueContentTypeOrder(IList`1 value)     at Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.ProvisionAllowedContentTypes().  18547c9e-93f6-d0b0-b463-a5a3d77c2eb1
Set EventFiringDisabled to [False].  Stack trace: [   at Microsoft.Office.Server.Utilities.EventReceiverUtility.DisabledEventFiringScope.Dispose()     at Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.Provision(Boolean bProvisionDefaultContent, SPLongOperationState longOperationState)     at Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.Provision(Boolean provisionDefaultContent, SPLongOperationState longOperationState, SPListItem item)     at Microsoft.Office.DocumentManagement.Pages.NewDocSet.<>c__DisplayClass3.<>c__DisplayClass5.<OKBtn_Click>b__1(SPLongOperationState longOperationState)     at Microsoft.SharePoint.SPStatefulLongOperation.SetThreadCultureAndRun(RunStatefulOperation runOperation, SPLongOperationState state, CultureInfo culture, CultureInfo uICu... 18547c9e-93f6-d0b0-b463-a5a3d77c2eb1
...lture)     at Microsoft.SharePoint.SPStatefulLongOperation.<>c__DisplayClass6.<Run>b__1(Object state)     at Microsoft.SharePoint.SPSecurity.RunAsUser(SPUserToken userToken, Boolean bResetContext, WaitCallback code, Object param)     at Microsoft.SharePoint.Utilities.SPThreadPool.WaitCallbackWrapper(Object state)     at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs)     at System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(IMessage msg, IMessageSink replySink)     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)     at System.Threading.ExecutionContext.Run(ExecutionContext executionCont...  18547c9e-93f6-d0b0-b463-a5a3d77c2eb1
...ext, ContextCallback callback, Object state, Boolean preserveSyncCtx)     at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()     at System.Threading.ThreadPoolWorkQueue.Dispatch()  ] 18547c9e-93f6-d0b0-b463-a5a3d77c2eb1
setSecurityScopeUnique: succeeded   1d547c9e-130e-d0b0-b463-a7363f7f4162
DocumentSet Provision Documents/;asdfjsaf;lsafl in ;asdfjsaf;lsafl : throws exception: <nativehr>0x80070005</nativehr><nativestack></nativestack>Access denied.. Stacktrace:    at Microsoft.SharePoint.SPGlobal.HandleUnauthorizedAccessException(UnauthorizedAccessException ex)     at Microsoft.SharePoint.Library.SPRequest.GetFileAndFolderProperties(String bstrUrl, String bstrStartUrl, ListDocsFlags ListDocsFlags, Boolean bThrowException, Int32& phrStatus, Object& pvarFiles, Object& pvarDirs, UInt32& pdwNumberOfFiles, UInt32& pdwNumberOfDirs)     at Microsoft.SharePoint.SPWeb.GetFileOrFolderProperties(String strUrl, ListDocsFlags listDocsFlags, Boolean throwException, SPBasePermissions& permMask)     at Microsoft.SharePoint.SPFolder.PropertiesCore(Boolean throwException)     at Microsoft.Share... 18547c9e-93f6-d0b0-b463-a5a3d77c2eb1
...Point.SPFolder.set_UniqueContentTypeOrder(IList`1 value)     at Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.ProvisionAllowedContentTypes()     at Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.Provision(Boolean bProvisionDefaultContent, SPLongOperationState longOperationState)     at Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.Provision(Boolean provisionDefaultContent, SPLongOperationState longOperationState, SPListItem item).   18547c9e-93f6-d0b0-b463-a5a3d77c2eb1
Leaving Monitored Scope (SPSqlClient). Execution Time=502.428063929171  1d547c9e-130e-d0b0-b463-a7363f7f4162
setSecurityScopeUnique: finished    1d547c9e-130e-d0b0-b463-a7363f7f4162

Why am I sometimes getting an Access Denied error, sometimes a NullReferenceException, and sometimes it just works fine?

My guess is that due to the Asynchronous nature of the event, SharePoint code is running while the Custom Code is running to change permissions, which causes issues. Fe: The creator of the package is redirected at the point in time that all permissions have been removed, which causes the Access Denied. If that's the case, would running the event Synchronously solve the issue?

Your Answer

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

Browse other questions tagged or ask your own question.