none
Accessing SPSite RRS feed

  • Question

  • Hello All,

    Simple question,

    Have a look at the code

             Dim oSite As SPSite = New SPSite("some site")
                Dim oSubSites As SPWebCollection = ygSite.AllWebs
                Dim sourceSite As SPWeb

              Dim siteURL As String

     For Each sourceSite In oSubSites
       If something = something Then
                  siteURL = sourceSite.Url
        End If
    Next sourceSite

    ' Now I want to open this site so I am passing the site URL which I collected using a string type variable.

    Dim tSite as SPSite = New SPSite ("siteURL")

    But This line gives me this error - the uri is no longer valid...

    Can you suggest how can get this operation done. I know passing a string type is causing the problem (i think) can you all advise how to do this so I can get the desired site url and pass it open and access it?

    Thanks in advance

    Friday, November 6, 2009 11:07 AM

Answers

  • Replact it:
    siteURL = sourceSite.Url
    with:
    siteURL = sourceSite.ServerRelativeUrl


    and why you are using quotes here:
    Dim tWeb As SPWeb = tSite.OpenWeb("siteURL")       " here I open the web
    ???
    • Marked as answer by .Net Frenzy Friday, November 6, 2009 4:36 PM
    Friday, November 6, 2009 12:24 PM
  • Oh, sorry, I was misled by your object names. :-)

    Why do you declare an object of type SPWeb called sourceSite and similar? I don't think the OM would be fussy. It really requires some experience, but applying bad coding styles and practices you can make it very hard to use and debug and even to understand what you wanted to do originally. But it is not specific to SharePoint OM only.

    Then it might be even more simple:

    Dim tWeb As SPWeb = Nothing
    Dim tSubWebs As SPWebCollection = tSite.AllWebs
    Dim sourceWeb As SPWeb

    ' Loop through site collection
    For Each sourceWeb In tSubWebs
    ' Check against checkbox selection
        If cbl.SelectedItem.Value.ToString = sourceWeb.Title.ToString Then
            tWeb = sourceWeb
        End If
    Next sourceSite

    ...

    If Not tWeb is Nothing Then
    ...

    Hope it is better. And I hope if it happen to not compile at first, you understood my approach and will be able to fix the possible errors.

    Peter


    • Marked as answer by .Net Frenzy Friday, November 6, 2009 4:35 PM
    Friday, November 6, 2009 3:24 PM

All replies

  • You have collected URLs for SPWeb objects and trying to do intstance of SPSite object!
    Friday, November 6, 2009 11:13 AM
  • ok,

    Can anyone provide with sample code that would get to work what I want to do.

    Thanks,
    Friday, November 6, 2009 11:15 AM
  • You need to open the same SPSite what was at the beggining
    Dim oSite As SPSite = New SPSite("some site")

    and then open Web with this Url
    oSite.OpenWeb(siteUrl)

    or you need to rembmer SPSite Url if you want to get instance of SPSite object
    • Marked as answer by .Net Frenzy Friday, November 6, 2009 11:29 AM
    • Unmarked as answer by .Net Frenzy Friday, November 6, 2009 11:30 AM
    • Marked as answer by .Net Frenzy Friday, November 6, 2009 11:31 AM
    • Unmarked as answer by .Net Frenzy Friday, November 6, 2009 11:40 AM
    Friday, November 6, 2009 11:20 AM
  • Got Stuck Again...

                    Dim tSite As SPSite = New SPSite("some site") - i open the same site as you mentioned
                    Dim tWeb As SPWeb = tSite.OpenWeb("siteURL")

                    tSite.AllowUnsafeUpdates = True
                    Dim tList As SPList = tWeb.Lists("My List")
                    Dim tListItem As SPListItem = workerList.Items.Add

                    ' i am simply adding some item to my list on that site (siteURL)

    now I get the error - "There is no web named "/siteURL"

    Can you help .. yet again..

    Friday, November 6, 2009 11:43 AM
  • Please provide me a full you code. I think you need to save ServerRelativeUrl, not just Url.
    Show you code like it is, without any "some site"
    Friday, November 6, 2009 11:47 AM
  • When you try to call site.OpenWeb('Url').. While passing Url of sub site, try passing relative weburl..
    Ashwin B. | My Blog | Twitter | E-Mail
    Friday, November 6, 2009 11:50 AM
  • Here have a look at the code..

    I am searching a site from siteCollection based on a checkboxlist selected item
    Once found then I need to update a list on that site

    --------------------------------------
     ' Fetching lists of all subsites within the Youth Group Site Collection
                Dim tSite As SPSite = New SPSite("something")
                Dim tSubSites As SPWebCollection = tSite.AllWebs
                Dim sourceSite As SPWeb

                Dim Notification As Boolean
                Dim siteURL As String

                ' Loop through site collection

     
    PS - I know I have tSite declared many times but on my code that is not the case.. this is merely just to make it quick..
    Please help - how to get that site and then work on that list.. Object Model Pain..!

    • Edited by .Net Frenzy Friday, November 6, 2009 4:32 PM
    Friday, November 6, 2009 12:09 PM
  • Replact it:
    siteURL = sourceSite.Url
    with:
    siteURL = sourceSite.ServerRelativeUrl


    and why you are using quotes here:
    Dim tWeb As SPWeb = tSite.OpenWeb("siteURL")       " here I open the web
    ???
    • Marked as answer by .Net Frenzy Friday, November 6, 2009 4:36 PM
    Friday, November 6, 2009 12:24 PM
  • NOP... GOT THIS AGAIN..

    There is no Web named "/siteURL".


    I am about to pull my hair out..
    Friday, November 6, 2009 12:31 PM
  • You posted this exact post again only 4 HOURS later.

    Do this again and I will delete both posts. As it is I deleted only the second copy.

    There is absolutely NO justification for re-posting something just because you haven't got the answer in the first four hours. In this case there is even less justification because this thread is live.

    I've also deleted a couple of messages here that are trying to hurry people along. Again there is no justification for this. If you have nothing to say, don't post. Do not post just to say that you are waiting.


    If you are in such a hurry. Do NOT use forums at all. Ring Microsoft Customer Support Services.


    (Moderator)

    FAQ sites: (SP 2010) http://wssv4faq.mindsharp.com; (v3) http://wssv3faq.mindsharp.com and (WSS 2.0) http://wssv2faq.mindsharp.com
    Complete Book Lists (incl. foreign language) on each site.
    Friday, November 6, 2009 3:16 PM
  • Oh, sorry, I was misled by your object names. :-)

    Why do you declare an object of type SPWeb called sourceSite and similar? I don't think the OM would be fussy. It really requires some experience, but applying bad coding styles and practices you can make it very hard to use and debug and even to understand what you wanted to do originally. But it is not specific to SharePoint OM only.

    Then it might be even more simple:

    Dim tWeb As SPWeb = Nothing
    Dim tSubWebs As SPWebCollection = tSite.AllWebs
    Dim sourceWeb As SPWeb

    ' Loop through site collection
    For Each sourceWeb In tSubWebs
    ' Check against checkbox selection
        If cbl.SelectedItem.Value.ToString = sourceWeb.Title.ToString Then
            tWeb = sourceWeb
        End If
    Next sourceSite

    ...

    If Not tWeb is Nothing Then
    ...

    Hope it is better. And I hope if it happen to not compile at first, you understood my approach and will be able to fix the possible errors.

    Peter


    • Marked as answer by .Net Frenzy Friday, November 6, 2009 4:35 PM
    Friday, November 6, 2009 3:24 PM
  • Dear Moderator,

    Sorry for the mistake and won't happen again.
    Friday, November 6, 2009 3:34 PM
  • Ok Peter,

    Ill give that a go and get back to you on that. Please continue checking the post. I really do appreciate your help with this. You are right bad coding practices that's all. Actually I am new to SP and its API and with little experiene and lack of understanding it becomes difficult to get things right. I will update you on this ASAP. Many thanks.
    Friday, November 6, 2009 3:35 PM
  • Hi Peter,

    Yes, It worked. Can't believe it took me all day to figure this one out. I will try to read some good articles on the Object Model so I can learn from ground up. Many thanks for your pointers.

    I would also like to thank to 'Jevgeni Borozna' for taking out time to help me on this one.

    Many thanks everyone.
    Friday, November 6, 2009 4:35 PM
  • Hi,

    Yes, learning may take long time. I'm happy to hear that it works now, and really appreciate your feedback.

    Peter
    Friday, November 6, 2009 6:25 PM