Monday, February 23, 2015

Adding Content Menu and Custom Menu items on a tree node - Windows Application using C#

This code shows how you can add a context menu with the context menu items to your window application on a tree node.

ToolStripMenuItem siteSettings = new ToolStripMenuItem();
            siteSettings.Text = "Site Settings";
            siteSettings.BackColor = Color.AliceBlue;
            siteSettings.Image = imageList1.Images[5];

            ToolStripMenuItem lstContentTypeReport = new ToolStripMenuItem();
            lstContentTypeReport.Text = "Lists Content Types";
            lstContentTypeReport.BackColor = Color.AliceBlue;
            lstContentTypeReport.Image = imageList1.Images[5];

            ToolStripMenuItem treeStructure = new ToolStripMenuItem();
            treeStructure.Text = "Site Structure";
            treeStructure.BackColor = Color.AliceBlue;
            treeStructure.Image = imageList1.Images[15];


            ToolStripMenuItem manageAuditSettings = new ToolStripMenuItem();
            manageAuditSettings.Text = "Manage Audit Settings";
            manageAuditSettings.BackColor = Color.AliceBlue;
            manageAuditSettings.Image = imageList1.Images[15];

            ToolStripMenuItem manageSPAlerts = new ToolStripMenuItem();
            manageSPAlerts.BackColor = Color.AliceBlue;
            manageSPAlerts.Text = "Manage ShaerPoint Alerts";
            manageSPAlerts.Image = imageList1.Images[14];

            ToolStripMenuItem addUsers = new ToolStripMenuItem();
            addUsers.Text = "Add Users";
            addUsers.BackColor = Color.AliceBlue;
            addUsers.Image = imageList1.Images[13];

            ToolStripMenuItem siteStorageAnalysis = new ToolStripMenuItem();
            siteStorageAnalysis.Text = "Site Storage Analysis";
            siteStorageAnalysis.BackColor = Color.AliceBlue;
            siteStorageAnalysis.Image = imageList1.Images[12];

            ToolStripMenuItem sitePemissions = new ToolStripMenuItem();
            sitePemissions.Text = "Site Permissions";
            sitePemissions.BackColor = Color.AliceBlue;
            sitePemissions.Image = imageList1.Images[11];

            ToolStripMenuItem findUserSecurity = new ToolStripMenuItem();
            findUserSecurity.Text = "Find User Security";
            findUserSecurity.BackColor = Color.AliceBlue;
            findUserSecurity.Image = imageList1.Images[10];
            contextMenuStrip2.Items.AddRange(new ToolStripMenuItem[] { siteSettings, lstContentTypeReport, manageAuditSettings, treeStructure, manageSPAlerts, addUsers });
            contextMenuStrip2.Items.Add(new ToolStripSeparator());
            contextMenuStrip2.Items.AddRange(new ToolStripMenuItem[] { siteStorageAnalysis, sitePemissions, findUserSecurity });



                Treenode Control:


                         TreeNode Groups = new TreeNode("Sites");                     
                  

                        Groups.ContextMenuStrip = groupContextMenu;

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);
                clientContext.Load(spp);
                Web web = clientContext.Web;
                clientContext.Load(web);
                clientContext.Load(web.Webs);
                clientContext.Load(clientContext.Web.SiteGroups);
                clientContext.Load(clientContext.Web.SiteUsers);
                clientContext.ExecuteQuery();

                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();
                }
               
            }