由于对象的当前状态,操作无效。(SPWebApplication.Update)

Posted

技术标签:

【中文标题】由于对象的当前状态,操作无效。(SPWebApplication.Update)【英文标题】:Operation is not valid due to the current state of the object.( SPWebApplication.Update) 【发布时间】:2014-05-27 16:27:59 【问题描述】:

我在尝试以编程方式在 sharepoint 2010 Web 应用程序中添加用户策略时收到此错误:

Operation is not valid due to the current state of the object.

这是一个代码示例:

public Boolean AddUser(SoftwareUser user)
    
        SPSecurity.RunWithElevatedPrivileges(delegate()
        
            using (site = new SPSite(siteUrl))
            
                using (web = site.RootWeb)
                
                    web.AllowUnsafeUpdates = true;
                    SPUser spUser = web.EnsureUser(user.UserShortname);
                    web.Groups["MySolution Members"].AddUser(spUser);
                    if (spUser != null)
                    
                        if (user.Email != null)
                        
                            spUser.Email = user.Email;
                        
                        spUser.Update();
                        SPWebApplication webApp = site.WebApplication;
                        SPPolicy policy = webApp.Policies.Add(spUser.Name, "deny");
                        policy.PolicyRoleBindings.Add(webApp.PolicyRoles["deny"]);
                        webApp.Update();
                    
                    web.AllowUnsafeUpdates = false;
                
            
        );
        return true;
    

提前谢谢你。


在堆栈跟踪下方查找:

System.InvalidOperationException: Operation is not valid due to the current state of the object.

System.InvalidOperationException: Operation is not valid due to the current state of the object.
at Microsoft.SharePoint.WebControls.SPControl.SPWebEnsureSPControl(HttpContext context)
at Microsoft.SharePoint.Utilities.SPUtility.ValidateFormDigest()
at Microsoft.SharePoint.Administration.SPPersistedObject.BaseUpdate()
at Microsoft.SharePoint.Administration.SPWebApplication.Update()
at WcfServices.WFService.<>c__DisplayClass2c.<AddUser>b__2b() in C:\SourceCode\DEV-_V5.2.5\WCFServices\SP2010\WcfServices\WcfServices\WFService.svc.cs:line 980
at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2()
at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)
at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param)
at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)
at WcfServices.WFService.AddUser(SoftwareUser user) in C:\SourceCode\DEV-_V5.2.5\WCFServices\SP2010\WcfServices\WcfServices\WFService.svc.cs:line 959
at SyncInvokeAddUser(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

【问题讨论】:

在哪一行出现错误? 你能提供一个堆栈跟踪吗? 【参考方案1】:

不要在 Elevated Permission 的委托中调用 .Update() 就在SPSecurity.RunWithElevatedPrivileges下面做吧

【讨论】:

以上是关于由于对象的当前状态,操作无效。(SPWebApplication.Update)的主要内容,如果未能解决你的问题,请参考以下文章

由于对象的当前状态,操作无效 - Linq on List

条件评估失败。规则项 ID:未知,条件项 ID:未知 - 由于对象的当前状态,操作无效

TCPClient iFix 错误:当前状态下的无效操作 40020

关于System.InvalidOperationException异常

datagridview上的不稳定InvalidOperationException

如何使用 NHibernate 迭代整个表?