in

ASP.NET Weblogs

Tolga Koseoglu

Building SharePoint Tools (Part 1)

In my previous post I wrote about how to get up-to-speed with a SharePoint installation and web part development. I talked about installation challenges and what helpful tools to use when trying to development SharePoint web parts. Now that your environment is ready I need some tool to discover and explore it. In other words we need to build us tools that utilize the SharePoint object model or SharePoint's web services.

Part 1 or Show me all existings sites (webs) in an existing SharePoint environment 

So, this is a basic C# forms application with references Microsoft.SharePoint.dll. Here is the code...

private void btnGetSourceSites_Click(object sender, EventArgs e)

        {
            this.tvSites.Nodes.Clear();
 
            //OM
 
            this.BuildSitesTreeFromOM();
 
            //WEB SERVICE
            tn = new TreeNode();
            tn.Text ="Root";
            this.tvSites.Nodes.Add(tn);
            this.BuildSitesTreeFromService(this.txtSourceSC.Text, tn);
            this.tvSites.ExpandAll();
        }
 
        //OM
        private void BuildSitesTreeFromOM()
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite spsite = new SPSite(this.txtSourceSC.Text))
                    {
                        SPWeb spweb = spsite.OpenWeb();
                        tn = new TreeNode();
                        tn.Text = spweb.Title;
                        tn.Tag = spweb.Url;
                        this.tvSites.Nodes.Add(tn);
                        this.GetChildSites(spweb, tn);
                        this.tvSites.ExpandAll();
                    }
 
                });
             
            }
            catch { }
        }
        private void GetChildSites(SPWeb _subsite, TreeNode _tn)
        {
            foreach (SPWeb subsite in _subsite.Webs)
            {
                tn = new TreeNode();
                tn.Text = subsite.Title;
                tn.Tag = subsite.Url;
                _tn.Nodes.Add(tn);
                this.GetChildSites(subsite, tn);
            }
 
        }
 
        //WEB SERVICE
        private void BuildSitesTreeFromService(String _url, TreeNode _tn)
        {
            WSS_Webs.Webs service = new WSS_Webs.Webs();
            service.PreAuthenticate = true;
            //service.Credentials = System.Net.CredentialCache.DefaultCredentials;
            service.Credentials = new System.Net.NetworkCredential("***", "***", "***");
            service.Url = _url + @"/_vti_bin/webs.asmx";
 
            XmlNode sites = null;
 
            try
            {
                sites = service.GetWebCollection();
            }
            catch
            {
                return;
            }
 
            foreach (System.Xml.XmlNode subsite in sites.ChildNodes)
            {
                tn = new TreeNode();
                tn.Text = subsite.Attributes["Title"].Value;
                tn.Tag = subsite.Attributes["Url"].Value;                
                _tn.Nodes.Add(tn);
                BuildSitesTreeFromService(subsite.Attributes["Url"].Value, tn);
            }
 
        }

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add

About kemaltolga

Born in Ankara, Turkey. Raised in Germany. Recruited for the Track & Field Team at Texas A&M University in College Station, TX, in January 2000. NCAA champion in 2001 (Discus). Work for design firm in southern California. Married with two daughters.