Wednesday 5 June 2013

How to enumerate all ‘Inherited Security’ sites


How to enumerate all ‘Inherited Security’ sites

This article is nothing new to SharePoint developers. In fact, there are better options of getting a listing of all subsites in your site collection. However, for those who are not experienced developers, this article will give you an idea of what you can do with the SharePoint web services.

Referencing the SharePoint “Webs.asmx” web service

In order to get a listing of all SharePoint subsites in a SharePoint site collection, you will have to call the ‘webs.aspx’ web service:
        private void GetAllInheritedSecuritySites()
        {
            string inheritedSites = string.Empty();
            XmlNode xmlNodes = null;

            WSS3_WEBS.Webs service = new WSS3_WEBS.Webs();
            service.Url = @tbSiteCollection.Text + @"/_vti_bin/Webs.asmx";
            service.UseDefaultCredentials = true;

            try
            {
                xmlNodes = service.GetAllSubWebCollection();
                //MessageBox.Show(xmlNodes.OuterXml);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Service Error" + ex.ToString());
            }

            foreach (XmlNode node in xmlNodes.ChildNodes)
            {
                inheritedSites += InheritedSite(node.Attributes["Url"].Value) + "--rn---";
            }
        }
Notice that the ‘foreach’ loop is looping thru all SharePoint subsites and is then calling the ‘InheritedSite’ method, passing in the URL to get the properties of the SharePoint site.

Referencing the SharePoint “SiteData.asmx” web service

Next, you will need to call the ‘sitedata.asmx’ web service for each SharePoint subsite found.
        private string InheritedSite(string _siteUrl)
        {
            string inheritedSite = string.Empty;
            XmlNode xmlNodes = null;

            WSS3_SITEDATA.SiteData service = new WSS3_SITEDATA.SiteData();
            service.Url = _siteUrl + @"/_vti_bin/SiteData.asmx";
            service.UseDefaultCredentials = true;

            WSS3_SITEDATA._sWebMetadata webMeta;
            string sRoles = string.Empty;
            string[] vRolesGroups;
            string[] vRolesUsers;
            WSS3_SITEDATA._sWebWithTime[] vWebs;
            WSS3_SITEDATA._sListWithTime[] vLists;
            WSS3_SITEDATA._sFPUrl[] vFPUrls;

            try
            {
                service.GetWeb(out webMeta, out vWebs, out vLists, out vFPUrls, out sRoles, out vRolesUsers, out vRolesGroups);
                switch (chkInheritedSecurity.Checked)
                {
                    case true:
                        if (webMeta.InheritedSecurity.ToString() == "True")
                        {
                            inheritedSite = _siteUrl + "rn";
                        }
                        break;
                    case false:
                        if (webMeta.InheritedSecurity.ToString() == "False")
                        {
                            inheritedSite = _siteUrl + "rn";
                        }
                        break;
                }
            }

            catch (Exception ex)
            {
                inheritedSite = _siteUrl + ": " + ex.ToString();
            }
            return inheritedSite;
        }
As mentioned before, there are much cleaner ways to get a listing of all your SharePoint sites that are inheriting permissions. This example is querying for this information by communicating with the Microsoft SharePoint web services and is only one way of many that you might want to consider.  Another option is to utilize the SharePoint Object Model by creating a web part.
Hope this helps.

No comments:

Post a Comment