Please help with exception while reading SPWeb.Id immediately after OpenWep()

Answered Please help with exception while reading SPWeb.Id immediately after OpenWep()

  • Tuesday, November 02, 2010 10:02 PM
     
      Has Code

    I am collecting a list of sites and their id. The following code throws  a FileNotFoundExcetion but only when run at full speed.  when run in a debugger I can read the property if I pause briefly after the OpenWeb() Call.

    SPWeb deptsite = site.OpenWeb(SiteURL);

    ListItem["SiteId"] = deptsite.ID.ToString();

     Is there someting I should be calling to ensure the deptsite object is ready?

    I can put a delay in the loop and it works but that is a hack that I wish to avoid.

    Thanks,Matt.

     

All Replies

  • Wednesday, November 03, 2010 2:45 PM
     
     

    Surprise, the Threaad.Sleep(10000) didnt work. 

    I broke a golden rule here but it seems that this form of Re-Try works.

    SPWeb deptsite = site.OpenWeb(SiteURL);

    Try

    {ListItem["SiteId"] = deptsite.ID.ToString();}

    catch

    {ListItem["SiteId"] = deptsite.ID.ToString();}

     

  • Wednesday, November 03, 2010 3:11 PM
     
     

    Maybe your server runs instable beacuse you don't dispose your object?

    Try somthing like:

    using(SPWeb web = site.OpenWeb(MyWeb))
    {
        ListItem["item"] = web.ID.ToString();
    }

    I never needed a time out or try catch when looping through sites  yet.

  • Wednesday, November 03, 2010 3:26 PM
     
      Has Code
    Thanks, but I was using a "using" here is the full test function.
    the output always writes the "Retry:" line to the console
    <pre lang="x-c#">
      public void Run()
      {
       foreach (string s in SiteNames)
       {
        using (SPWeb w = site.AllWebs[s])
        {
         try
         {
          //The next line throws an exception but NOT when
          //stepping through with debugger     
          Console.WriteLine("Try:" + w.ID.ToString());
         }
         catch (Exception ex)
         {
          Console.WriteLine("Retry:" + w.ID.ToString());
         }
        }
       }//put a breakpoint on this line and the exception will happen on 
        //the writeline statement
    
       Console.ReadKey();
      }
    
  • Thursday, November 04, 2010 9:35 AM
     
     Answered Has Code
    I ran your code without any problems:
      public Program()
      {
       String[] SiteNames = new String[5]{"a","b","c","d","e"};
       using (SPSite site = new SPSite("http://MyUrl.be/sites/test_312"))
       {
        foreach (string s in SiteNames)
        {
         using (SPWeb w = site.AllWebs[s])
         {
          try
          {
           //The next line throws an exception but NOT when
           //stepping through with debugger  
           Console.WriteLine("Try:" + w.ID.ToString());
          }
          catch (Exception ex)
          {
           Console.WriteLine("Retry:" + w.ID.ToString());
          }
         }
        }//put a breakpoint on this line and the exception will happen on 
        //the writeline statemen
       }
      }
    • Marked As Answer by Wayne Fan Friday, November 12, 2010 2:51 AM
    •  
  • Thursday, November 04, 2010 2:13 PM
     
     

    Are you saying that you get the console display of the form:

    Try:guid

    or

    Retry:guid

     

  • Thursday, November 04, 2010 3:29 PM
     
     Answered

    Try:guid

    so eveyrthing works as expected.

    • Marked As Answer by Wayne Fan Friday, November 12, 2010 2:51 AM
    •