Monday, February 23, 2015

SharePoint Online - Connect and fetch information usign client side object model (C#)

Below code example shows how to connect to a tenant application and iterate through with all the site collections.

Tip: Assuming you have permission to read content to all site collection under this tenant.
 If not, use proper try catch with continue option. J

Step 1 - Add reference for the following assemblies. 

using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;

Step 2 -
string userName = "";
string passWord = "";

using (ClientContext clientContext = new ClientContext(txtTenantUrl.Text))

                SecureString passWord = new SecureString();
           foreach (char c in password.ToCharArray()) passWord.AppendChar(c);
           clientContext.Credentials = new SharePointOnlineCredentials(userName, passWord);

                Tenant tenant = new Tenant(clientContext);
                SPOSitePropertiesEnumerable spp = tenant.GetSiteProperties(0, true);
                Web web = clientContext.Web;

                foreach (SiteProperties sp in spp)
                              string owner = sp.Owner.ToString();
                    string URL = sp.Url.ToString();
                    string webCount = sp.WebsCount.ToString();
                    string webLastModified = sp.LastContentModifiedDate.ToString();
                    string webStatus = sp.Status.ToString();
                    string webStorage = sp.StorageUsage.ToString();
                    string level = sp.StorageMaximumLevel.ToString();
string contentLastModified = sp.LastContentModifiedDate.ToString();

No comments: