1

我正在尝试在添加新项目但获得关注时更新项目安全性。如何从这个跟踪中找到问题?

InvalidOperationException: Operation is not valid due to the current state of the object.]
   Microsoft.SharePoint.SPListItem.InitSecurity() +26790031
   Microsoft.SharePoint.SPListItem.get_SecurableObjectImpl() +16
   Microsoft.SharePoint.SPSecurableObject.get_RoleAssignments() +10
   Updateroutine.<>c__DisplayClass2.<updatelistpermission>b__0() +1955
   Microsoft.SharePoint.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2() +729
   Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) +27947702
   Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param) +27648393
   Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode) +93

这是我的功能,

private static void updatelistpermission(Header objEntity, SPListItem lstItem,string strStatus)
        {

            SPUserToken token = lstItem.Web.Site.SystemAccount.UserToken;

            try
            {

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {

                    using (SPSite site = new SPSite(lstItem.Web.Site.ID, token))
                    {
                        site.AllowUnsafeUpdates = true;
                        using (SPWeb web = site.OpenWeb(lstItem.Web.ID))
                        {
                            web.AllowUnsafeUpdates = true;
                            lstItem.BreakRoleInheritance(false); 

                            SPRoleDefinition roleUserDefintion = null;
                            SPRoleDefinition roleSupervisorDefintion = null;
                            SPRoleDefinition roleOpManagerDefintion = null;

                            if (strStatus == "Active")
                            {
                                roleUserDefintion = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                                roleSupervisorDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                roleOpManagerDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                            }
                            else if (strStatus == "Submitted")
                            {
                                roleUserDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                roleSupervisorDefintion = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                                roleOpManagerDefintion = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                            }
                            else if (strStatus == "Completed")
                            {
                                roleUserDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                roleSupervisorDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                roleOpManagerDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                            }

                            SPUser uParticipant = Utility.convertStringToSPUser(Utility.getSPStringLookupFromListGLT(objEntity.Participant));
                            SPRoleAssignment roleUserAssignment = new SPRoleAssignment(uParticipant);

                            SPUser uSupervisor = Utility.convertStringToSPUser(Utility.getSPStringLookupFromListGLT(objEntity.Supervisor));
                            SPRoleAssignment roleSupervisorAssignment = new SPRoleAssignment(uSupervisor);

                            SPUser uOpManager = Utility.convertStringToSPUser(Utility.getSPStringLookupFromListGLT(objEntity.OpsManager));
                            SPRoleAssignment roleuOpManagerAssignment = new SPRoleAssignment(uOpManager);

                            roleUserAssignment.RoleDefinitionBindings.Add(roleUserDefintion);
                            lstItem.RoleAssignments.Add(roleUserAssignment);

                            roleSupervisorAssignment.RoleDefinitionBindings.Add(roleSupervisorDefintion);
                            lstItem.RoleAssignments.Add(roleSupervisorAssignment);

                            roleuOpManagerAssignment.RoleDefinitionBindings.Add(roleOpManagerDefintion);
                            lstItem.RoleAssignments.Add(roleuOpManagerAssignment);

                            lstItem.Update();
                            web.AllowUnsafeUpdates = false;
                        }
                    }
               });
            }
            catch (SPException spex)
            {
                SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("APP", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, spex.Message , "");
            }
        }

@Dmitry Kozlov 建议的新代码,但仍然是相同的错误

try
                {
                    {
                        SPUserToken token = SPContext.Current.Web.Site.SystemAccount.UserToken;

                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            using (SPSite site = new SPSite(SPContext.Current.Web.Site.ID, token))
                            {
                                site.AllowUnsafeUpdates = true;

                                using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
                                {
                                    web.AllowUnsafeUpdates = true;
                                    web.BreakRoleInheritance(true);
                                    web.AllowUnsafeUpdates = true;

                                    SPList lstProject = web.Lists["SharedSurveyHeader"];
                                    SPListItem lstItem = lstProject.Items.Add();
                                    loadToListItem(sharedHeader, ref lstItem, true);
                                    updatelistpermission(web, sharedHeader, lstItem, strStatus);
                                    lstItem.Update();
                                    objWeb.AllowUnsafeUpdates = false;
                                    intResult = lstItem.ID;
                                    lstItem = null;
                                    lstProject = null;
                                }
                            }
                        });


                    }

                 }


private static void updatelistpermission(SPWeb web, SharedSurveyHeader objEntity, SPListItem lstItem,string strStatus)
        {
             try
            {

                            SPRoleDefinition roleUserDefintion = null;
                            SPRoleDefinition roleSupervisorDefintion = null;
                            SPRoleDefinition roleOpManagerDefintion = null;

                            if (strStatus == "Active")
                            {
                                roleUserDefintion = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                                roleSupervisorDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                roleOpManagerDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                            }
                            else if (strStatus == "Submitted")
                            {
                                roleUserDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                roleSupervisorDefintion = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                                roleOpManagerDefintion = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                            }
                            else if (strStatus == "Completed")
                            {
                                roleUserDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                roleSupervisorDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                roleOpManagerDefintion = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                            }

                            SPUser uParticipant = Utility.convertStringToSPUser(Utility.getSPStringLookupFromListGLT(objEntity.Participant));
                            SPRoleAssignment roleUserAssignment = new SPRoleAssignment(uParticipant);

                            SPUser uSupervisor = Utility.convertStringToSPUser(Utility.getSPStringLookupFromListGLT(objEntity.Supervisor));
                            SPRoleAssignment roleSupervisorAssignment = new SPRoleAssignment(uSupervisor);

                            SPUser uOpManager = Utility.convertStringToSPUser(Utility.getSPStringLookupFromListGLT(objEntity.OpsManager));
                            SPRoleAssignment roleuOpManagerAssignment = new SPRoleAssignment(uOpManager);

                            roleUserAssignment.RoleDefinitionBindings.Add(roleUserDefintion);
                            lstItem.RoleAssignments.Add(roleUserAssignment);

                            roleSupervisorAssignment.RoleDefinitionBindings.Add(roleSupervisorDefintion);
                            lstItem.RoleAssignments.Add(roleSupervisorAssignment);

                            roleuOpManagerAssignment.RoleDefinitionBindings.Add(roleOpManagerDefintion);
                            lstItem.RoleAssignments.Add(roleuOpManagerAssignment);

            }
            catch (SPException spex)
            {

            }
        }
4

1 回答 1

0

尝试从您以提升的权限打开的 Web 获取列表项并使用它:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(lstItem.Web.Site.ID, token))
    {
        site.AllowUnsafeUpdates = true;
        using (SPWeb web = site.OpenWeb(lstItem.Web.ID))
        {
           web.AllowUnsafeUpdates = true;

           // Get list item here:
           var newListItem = web.GetListItem(itemUrl);
           ...
        }
    }
}
于 2012-10-30T19:14:11.290 回答