Jaa


SPWeb.GetObject() throws an exception when site context changes.

Here is the scenario, you may want to get an object(SPListItem) without iterating through the list item collection, if you know the URL of the file the GetObject() returns the item.

Section#1

    1: 1. SPSite site = new SPSite("https://nishand:100/sites/RootSite/");
    2: 2. SPWeb web = site.OpenWeb();
    3: 3. object ob1 = web.GetObject("https://nishand:100/sites/RootSite/SubSite/Shared%20Documents/Three.txt");
    4: 4. object ob2 = web.GetObject("https://nishand:100/sites/RootSite/SubSite/Shared%20Documents/Four.txt");
    5: 5. object ob3 = web.GetObject("https://nishand:100/sites/RootSite/Shared%20Documents/One.txt");

You will get an exception when you try calling the …One.txt file(line # 5)

Microsoft.SharePoint.SPException: Incorrect function. (Exception from HRESULT: 0x80070001) ---> System.Runtime.InteropServices.COMException (0x80070001): Incorrect function. (Exception from HRESULT: 0x80070001)

Workaround:

Section#2

    1: object ob2 = web.GetObject("https://nishand:100/sites/RootSite/SubSite/Shared%20Documents/Three.txt");
    2: web1 = site.OpenWeb();
    3: object ob2 = web.GetObject("https://nishand:100/sites/RootSite/SubSite/Shared%20Documents/Four.txt");
    4: web1 = site.OpenWeb();
    5: object ob3 = web.GetObject("https://nishand:100/sites/RootSite/Shared%20Documents/One.txt");

Note: MOSS 2007 SP1 restricts the user from accessing a resource outside the current context web, so in section#1 -line#3 will throw an exception, because the web context is https://nishand:100/sites/RootSite/ and the line#3 is trying to get an item from outside the current web.

 

Happy coding!!!!