Nishand's Blog

Welcome to my personal blog, All the information in this bogs is my ideas,findings and thoughts on .Net, Asp.Net and SharePoint.
ALL POSTING ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND

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("http://nishand:100/sites/RootSite/");
   2: 2. SPWeb web = site.OpenWeb();
   3: 3. object ob1 = web.GetObject("http://nishand:100/sites/RootSite/SubSite/Shared%20Documents/Three.txt");
   4: 4. object ob2 = web.GetObject("http://nishand:100/sites/RootSite/SubSite/Shared%20Documents/Four.txt");
   5: 5. object ob3 = web.GetObject("http://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("http://nishand:100/sites/RootSite/SubSite/Shared%20Documents/Three.txt");
   2: web1 = site.OpenWeb();
   3: object ob2 = web.GetObject("http://nishand:100/sites/RootSite/SubSite/Shared%20Documents/Four.txt");
   4: web1 = site.OpenWeb();
   5: object ob3 = web.GetObject("http://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 http://nishand:100/sites/RootSite/ and  the line#3 is trying to get an item from outside the current web.

 

Happy coding!!!!

Published Saturday, June 07, 2008 5:04 PM by nishandv

Comments

No Comments
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker