What is the scope of SPContext.Current.Web.GetList?
In my visual web part for sharepoint 2010, I use the function "SPContext.Current.Web.GetList("myserverrelativeurl")" to get a document library. But I noticed this only works if the link is in the same site as the web part. If I try a link to a document library from a child site or parent site, it will give an error with the stacktrace:

    (7/12/2013 12:17:08 PM) - Error Message: Incorrect function. (Exception from HRESULT: 0x80070001)
    Stacktrace:    at Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx)
       at Microsoft.SharePoint.Library.SPRequest.GetMetadataForUrl(String bstrUrl, Int32 METADATAFLAGS, Guid& pgListId, Int32& plItemId, Int32& plType, Object& pvarFileOrFolder)
       at Microsoft.SharePoint.SPWeb.GetList(String strUrl)



Is this normal behavior?

Thanks.

July 12th, 2013 12:23pm

quick answer.

if you use spcontext.current.web it clearly says the scope is current web.

you dont have to use server relative url on the current web to the get the current web list. instead you can use the list name.

spcontext.current.web.lists["listname"]

if you wanna get list method on differnt site collection then you need to use

spsite ssite=new spsite("site url")

{

spweb curweb=new spweb(site.openweb())

{

splist slist=curweb.lists["listname"];

}

}

Free Windows Admin Tool Kit Click here and download it now
July 12th, 2013 1:14pm

If I open a site and web using the using tag, then get the library object,  then save the library object to a global variable. Then let the site and web be disposed from the using tags, can I still get items from that library object?

July 12th, 2013 1:23pm

I guess you could if you really wanted to but why couldn't you just use the list while you have it in the using?
Free Windows Admin Tool Kit Click here and download it now
July 12th, 2013 2:03pm

There is no point in using the object to a global variable and then reconsuming it.

Its always the best pratice to dispose the object when not used.

July 12th, 2013 2:30pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics