0

I need an advice on sharepoint logs related to disposing objects.

I was looking in the logs and found these :

Unexpected Detected use of SPRequest for previously closed SPWeb object. Please close SPWeb objects when you are done with all objects obtained from them, but not before.

Stack trace:
    at Microsoft.SharePoint.SPFile.PropertiesCore(Boolean throwException) 
    at Microsoft.SharePoint.SPFile.get_CheckInComment() 
    ...

Monitorable An SPRequest object was not disposed before the end of this thread.

To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it. This object will now be disposed.

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() 
    ...

I can't figure out what is the main difference between these two codes as i dispose objects the same way.

What is wrong fith the For loop ?

Code which creates log error :

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
                }
            }
        }
    }
}

Code which does not create log error :

    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
                }
            }
        }
    }
}

I will need do use the For loop because i wil need to delete item in the enumeration, thing i won't be able to do with the foreach loop.

If you have any idea to help me please.

Thanks.

0

I haven't tested it but I think it is the usage of SPListItem currItem in items rather than loop type. If in first case you would do SPListItemsCollection items = lst.Items; and then use index to go through that collection you should work ok.

-1

Are you simply looking to delete the item in the list if publish is true?

If, so you can just collect the IDs or Unique IDs of each item and then run a foreach loop on each of the IDs and delete that item from the list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.