尝试以编程方式添加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;
}
提前谢谢。
在下面找到stacktrace:
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)
答案 0 :(得分:0)
请勿在Elevated Permissions委托中调用.Update()
只需在SPSecurity.RunWithElevatedPrivileges