首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SharePoint处理SPWeb、SPSite和对象

SharePoint处理SPWeb、SPSite和对象
EN

Stack Overflow用户
提问于 2016-10-05 13:07:45
回答 1查看 319关注 0票数 0

我需要关于与处理对象相关的sharepoint日志的建议。

我在查看日志时发现了这些:

意外检测到对以前关闭的SPWeb对象使用了SPRequest。请在处理所有从它们获得的对象时关闭SPWeb对象,但在此之前请关闭。

代码语言:javascript
复制
Stack trace:
    at Microsoft.SharePoint.SPFile.PropertiesCore(Boolean throwException) 
    at Microsoft.SharePoint.SPFile.get_CheckInComment() 
    ...

在此线程结束之前没有释放可监视的SPRequest对象。

为了避免浪费系统资源,在使用完该对象或其父对象(例如SPSite或SPWeb)之后,立即处理它。现在将释放此对象。

代码语言:javascript
复制
This SPRequest was allocated at
    at Microsoft.SharePoint.Library.SPRequest..ctor() 
    at Microsoft.SharePoint.SPGlobal.CreateSPRequestAndSetIdentity(SPSite site, String name, Boolean bNotGlobalAdminCode, String strUrl, Boolean bNotAddToContext, Byte[] UserToken, String userName, Boolean bIgnoreTokenTimeout, Boolean bAsAnonymous) 
    at Microsoft.SharePoint.SPWeb.InitializeSPRequest() 
    at Microsoft.SharePoint.SPWeb.GetFileOrFolderProperties(String strUrl, ListDocsFlags listDocsFlags, Boolean throwException, SPBasePermissions& permMask) 
    at Microsoft.SharePoint.SPFile.PropertiesCore(Boolean throwException) 
    at Microsoft.SharePoint.SPFile.get_CheckInComment() 
    ...

我不知道这两种代码之间的主要区别是什么,因为我以同样的方式处理对象。

For循环出什么问题了?

创建日志错误的代码:

代码语言:javascript
复制
using (SPSite site = new SPSite(url))
{
    using (SPWeb web = site.OpenWeb())
    {
        string serverRelativeUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, listRelativeUrl);
        SPList lst =  web.GetList(serverRelativeUrl);

        items = lst.Items;

        if (items != null)
        {
            int nb = items.Count;
            for (int i = nb - 1; i > -1; i--)
            {
                currItem =items[i];

                if (currItem.File.CheckInComment.Equals(comment1) || currItem.File.CheckInComment.Equals(comment2))
                {
                    publish = true;
                }

                if(publish)
                {
                    //do stuff with currItem data
                }
            }
        }
    }
}

不创建日志错误的代码:

代码语言:javascript
复制
    using (SPSite site = new SPSite(url))
{
    using (SPWeb web = site.OpenWeb())
    {
        string serverRelativeUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, listRelativeUrl);
        SPList lst =  web.GetList(serverRelativeUrl);

        items = lst.Items;

        if (items != null)
        {
            foreach(SPListItem currItem in items )
            {           
                if (currItem.File.CheckInComment.Equals(comment1) || currItem.File.CheckInComment.Equals(comment2))
                {
                    publish = true;
                }

                if(publish)
                {
                    //do stuff with currItem data
                }
            }
        }
    }
}

我需要使用For循环,因为我需要在枚举中删除项,这是我无法使用foreach循环做的事情。

如果你有什么办法帮我的话。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-10-05 14:08:37

我会在数字集合中捕获要删除的项的ID。一个数组或对象列表就足够了。

然后,先检查您的集合是否包含任何ID。然后执行

代码语言:javascript
复制
for(int i = 0, i < collection.count(), ++i){
 #get your item 
 #delete your item 
 #call the correct update method on your list.
}

我不能告诉你你为什么要处理这个问题。

干杯

特鲁兹

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39874850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档