Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Retrieve from Sharepoint 2010 list of all sites in VB

Posted on 2015-01-27
5
Medium Priority
?
335 Views
Last Modified: 2016-02-18
How can I retrieve the current list of all sites in Sharepoint through VB using the Imports Microsoft.SharePoint.Client ?
My goal is to check whether the site is already created before VB creates a new Site. I got the site creation down, but now I need to check and see if the site has already been created.
0
Comment
Question by:BKennedy2008
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 32

Expert Comment

by:Jamie McAllister MVP
ID: 40572962
The API calls are identical for VB, just the surrounding code is a little different.

Here's a C# example you can port easily.

using(SPWeb web = site.OpenWeb("/anotherWeb/"))
{
   if(web.Exists)
   {
       string title = web.Title;
   }
}

Open in new window

0
 

Author Comment

by:BKennedy2008
ID: 40573063
Can you put your logic within this code below taht creates the site? Thanks

 Dim clientContext As New ClientContext("http://sps.xxxx.com/")
        Dim collWeb As WebCollection = clientContext.Web.Webs

        Dim webCreationInfo As New WebCreationInformation()
        webCreationInfo.Title = Job_Number.Text
        webCreationInfo.Description = JobNme.Text
        webCreationInfo.Language = 1033
        webCreationInfo.Url = Job_Number.Text
        webCreationInfo.UseSamePermissionsAsParentSite = True
        webCreationInfo.WebTemplate = TemplateID

        Dim oNewWebsite As Web = collWeb.Add(webCreationInfo)
        Try
            clientContext.ExecuteQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
            Me.Cursor = Cursors.Default
            Exit Sub
        End Try
0
 
LVL 32

Accepted Solution

by:
Jamie McAllister MVP earned 2000 total points
ID: 40573187
Use of Client Object Model changes my answer a little;

 Private Function WebExists(ByVal siteUrl As String, ByVal webUrl As String) As Boolean
    'connect to the root site
    Imports (ClientContext context = New ClientContext(siteUrl))
    {
        ' load up the root web object but only 
        ' specifying the sub webs property to avoid 
        ' unneeded network traffic
        Dim web As var =  context.Web 
        context.Load(web, w =&gt w.Webs)
        context.ExecuteQuery()
        ' use a simple linq query to get any sub webs with the URL we want to check
        Dim subWeb As var = (from w in web.Webs where w.Url  =  webUrl select w).SingleOrDefault() 
        If Not subWeb Is Nothing Then
          ' if found true
          Return True
        End If
      }
      ' default to false...
      Return False
    End Function
 
 If Not WebExists("http://sps.xxxx.com/", "http://sps.xxxx.com/");
 
 Dim clientContext As New ClientContext("http://sps.xxxx.com/")
        Dim collWeb As WebCollection = clientContext.Web.Webs

        Dim webCreationInfo As New WebCreationInformation()
        webCreationInfo.Title = Job_Number.Text
        webCreationInfo.Description = JobNme.Text
        webCreationInfo.Language = 1033
        webCreationInfo.Url = Job_Number.Text
        webCreationInfo.UseSamePermissionsAsParentSite = True
        webCreationInfo.WebTemplate = TemplateID

        Dim oNewWebsite As Web = collWeb.Add(webCreationInfo)
        Try
            clientContext.ExecuteQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
            Me.Cursor = Cursors.Default
            Exit Sub
        End Try
		
		bool WebExists(string siteUrl, string webUrl)
{
    //connect to the root site
    using (ClientContext context = new ClientContext(siteUrl))
    {
        // load up the root web object but only 
        // specifying the sub webs property to avoid 
        // unneeded network traffic
        var web = context.Web;
        context.Load(web, w => w.Webs);
        context.ExecuteQuery();
        // use a simple linq query to get any sub webs with the URL we want to check
        var subWeb = (from w in web.Webs where w.Url == webUrl select w).SingleOrDefault();
        if (subWeb != null)
        {
          // if found true
          return true;
        }
      }
      // default to false...
      return false;
}
End If

Open in new window

0
 

Author Comment

by:BKennedy2008
ID: 40573850
I reduced it to this, and works great. Thanks for a starting point
Private Sub getSubWebs(path As String)
        Try
            Dim clientContext As New ClientContext(path)
            Dim oWebsite As Web = clientContext.Web
            clientContext.Load(oWebsite, Function(website) website.webs)
            clientContext.ExecuteQuery()

            For Each orWebsite As Web In oWebsite.Webs
                dt.Rows.Add(orWebsite.Title)
            Next
            DataGridView2.DataSource = dt
        Catch ex As Exception
        End Try


    End Sub
0
 

Author Comment

by:BKennedy2008
ID: 40573856
I've requested that this question be closed as follows:

Accepted answer: 0 points for BKennedy2008's comment #a40573850
Assisted answer: 500 points for Jamie McAllister MVP's comment #a40573187

for the following reason:

Modified to VB
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary In SharePoint 2010 it is easy to create custom color themes to jazz up a site. Theme colors can also be created in PowerPoint 2010 with a few clicks. But how do the chosen colors actually look in the SharePoint site? The attached PowerPoint…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question