首页 新闻 搜索 专区 学院

Asp.net网站访问Sharepoint2010报错:Operation is not valid due to the current state of the object

0
悬赏园豆:50 [待解决问题]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn_OK_Click(object sender, EventArgs e)
    {
        using(SPSite sitecollection=new SPSite("http://localhost"))
        {
            using(SPWeb web=sitecollection.RootWeb)
            {
                SPUserCollection users = web.SiteUsers;
                this.lbl_alertinfo.Text = users.Count.ToString();
             }
        }
    }
}

首先,确定http://localhost是正确的,我测试过,可以取到该SPWeb的标题,但执行this.lbl_alertinfo.Text = users.Count.ToString();报错,详细错误如下:

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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


Stack Trace:

[InvalidOperationException: Operation is not valid due to the current state of the object.]
   Microsoft.SharePoint.WebControls.SPControl.EnsureSPWebRequest(SPWeb web) +218
   Microsoft.SharePoint.WebControls.SPControl.SPWebEnsureSPControl(HttpContext context) +520
   Microsoft.SharePoint.Utilities.SPUtility.DetermineLayoutsUrl(SPWeb overrideWeb, HttpContext context, Boolean includeLCID, Boolean doNotInitWeb) +252
   Microsoft.SharePoint.Utilities.SPUtility.DetermineRedirectUrl(String urlProposed, SPRedirectFlags flags, HttpContext context, SPWeb overrideWeb, String queryString, String& urlRedirect) +616
   Microsoft.SharePoint.Utilities.SPUtility.Redirect(String url, SPRedirectFlags flags, HttpContext context, String queryString) +98
   Microsoft.SharePoint.Utilities.SPUtility.HandleAccessDenied(HttpContext context) +1077
   Microsoft.SharePoint.Utilities.SPUtility.HandleAccessDenied(Exception ex) +27952511
   Microsoft.SharePoint.Library.SPRequest.GetUsersDataAsSafeArray(String bstrUrl, UInt32 dwUsersScope, String bstrValue, UInt32 dwValue, UInt32& pdwColCount, UInt32& pdwRowCount, Object& pvarDataSet) +28050001
   Microsoft.SharePoint.SPUserCollection.InitUsers(Boolean fCustomUsers, String[] strIdentifiers) +301
   Microsoft.SharePoint.SPUserCollection.get_Count() +55
   Login.btn_OK_Click(Object sender, EventArgs e) +363
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +115
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +140
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981

 

请问是什么问题?

史向平的主页 史向平 | 初学一级 | 园豆:164
提问于:2012-09-12 21:41
< >
分享
所有回答(1)
0

应该是Permission问题吧,Sharepoint的permission比较恶心,可能是你目前登陆的账户没有权限获得所有其他用户的详细信息,你是用的管理员权限执行的这段代码吗?你可以试下run as adminstrator ,希望能帮到你

强者运强 | 园豆:338 (菜鸟二级) | 2012-09-13 21:27

情况是这样的:

在VS中运行,直接报错:“找不到web应用程序”,网上说只能发布到IIS后方能运行。

发布到IIS后能正常运行,但报此问题(如题),还没办法调试,因为不是在VS里运行的,悲剧。

支持(0) 反对(0) 史向平 | 园豆:164 (初学一级) | 2012-09-14 09:20

那你怎么知道是“但执行this.lbl_alertinfo.Text =users.Count.ToString();报错”的啊?@史向平: 

支持(0) 反对(0) 强者运强 | 园豆:338 (菜鸟二级) | 2012-09-14 21:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册