4

I need to retrieve a list of all the SharePoint sites to which I have access to via a windows application (C#). I am planning to use SharePoint web services.

Any pointers using SharePoint web services which could provide me the required information?

6 Answers 6

5

If you want to use the API instead then I would suggest you do the following to return all the sub webs for the current user without having to use elevated priviliges.

using(SPSite site = new SPSite("http://example/site/"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPWebCollection webCollection = web.GetSubwebsForCurrentUser();
    }
}
0
3

Webs.asmx should do the trick. Here's a snippet to get you started.

Dim rootNode As XmlNode = Nothing

Using ws As New WebsProxy.Webs
    ws.PreAuthenticate = True
    ws.UseDefaultCredentials = True
    ws.Url = <site collection address> + "/_vti_bin/webs.asmx"
    rootNode = ws.GetWebCollection()
End Using
2

I know this question is really old, but since it's still one of the first/only results I thought I would share the solution I found that uses SharePoint search to solve the problem. Not only is it really fast, you can tweak the query to your liking or even create a custom search scope to limit the results.

string queryText = "SELECT url, title " +
                    "FROM Scope() " +
                    "WHERE \"Scope\" = 'All Sites' " +
                    "AND (ContentClass = 'STS_Site' OR ContentClass = 'STS_Web')";

SearchServiceApplicationProxy proxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(SPContext.Current.Site));
FullTextSqlQuery searchQuery = new FullTextSqlQuery(proxy);
searchQuery.ResultsProvider = SearchProvider.Default;
searchQuery.ResultTypes = ResultType.RelevantResults;
searchQuery.EnableStemming = false;
searchQuery.TrimDuplicates = true;
searchQuery.QueryText = queryText;
searchQuery.RowLimit = 1000; 
ResultTableCollection results = searchQuery.Execute();
ResultTable result = results[ResultType.RelevantResults];

while (result.Read())
{
    string url = result.GetString(0);
    string title = result.GetString(1);

    ...
}

The query above can also be passed to /vti_bin/search.asmx, but that's a little more complicated. More info can be found here: http://msdn.microsoft.com/en-us/library/ee872313.aspx

0
2

Using the SharePoint .NET SDK, I was able to pull all sites the logged in user has access to by sending a query against the WebTemplate:GROUP keyword.

Here is my working example against SharePoint online (at time of this writing called Office 365 SharePoint):

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Search.Query;

// Connection to SharePoint.
var context = new ClientContext(url);

var securePassword = new System.Security.SecureString();
foreach (var c in password.ToCharArray())
{
    securePassword.AppendChar(c);
}
context.Credentials = new SharePointOnlineCredentials(username, securePassword);

// Pull sites the user has access to.
var query = new KeywordQuery(context);
query.QueryText = "WebTemplate:GROUP";
query.SelectProperties.Add("Title");
query.SelectProperties.Add("Path");
query.RowsPerPage = 1000;

var results = new SearchExecutor(context).ExecuteQuery(query);
context.ExecuteQuery();

// Process results...
// (May want to add some error/null condition checks.)
var resultTable = results.Value.First();
foreach (var result in resultTable.ResultRows)
{
    Console.WriteLine(string.Format("Title: {0} -- Path: {1}",
        result["Title"].ToString(),
        result["Path"].ToString()));
}
0
0

There's no way to do this with the out of the box web services I think. You could however write your web service, deploy that to your sharepoint farm and then call that service.

In it should be a method that takes a username and then using SPSecurity.RunWithElevatedPriviliges loops through the site collections / sites to determine if the user provided has access to each.

0

Looking for a similar solution, I came across SharePoint SUSHI on CodePlex. I haven't tried it yet, but it looks like it'll do what you're looking for. Or if you really want to write your own, you could check out the code and see how they're doing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.