in

ASP.NET Weblogs

Tolga Koseoglu

August 2009 - Posts

  • 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);
                }
     
            }
  • SharePoint (MOSS) Installation and preparation for development

    So, for the 20th time I am getting a development prepped for MOSS/WSS development. And, again, I forget how to get everything configured and installed. Hopefully this post will save some of you time in doing this.

    1. Install MOSS on Server 2008. Not straight forward. In order to get the install working you need to SlipStream the service packs for WSS and MOSS. (use command tool with the /extract command)
    2. Once MOSS is installed and you created a site collection you are ready to get the development environment ready. I recommend, Jan Tielens' Smart Part Templates.
    3. Make sure to select "SharePoint Web Part" not "Smart Part".
    4. Build the project and run the set-up.
    5. In MOSS, find "Site Collection Features" and activate your feature.
    6. If you database stuff, set trust level to full in web.config file
    7. If you like to load an .ascx file here is some code...

    WebPart code

    namespace SharePointWebPart1
    {
        public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
        {       
            private System.Web.UI.Control control = null;       
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
                control = this.Page.LoadControl(@"\WATG\MyControl.ascx");
                this.Controls.Add(control);
            }
            protected override void Render(System.Web.UI.HtmlTextWriter writer)
            {
                control.RenderControl(writer);
            }
        }
    }

    .Ascx Html 

    <%@ Control Language="C#" AutoEventWireup="true" Inherits="SharePointWebPart1.MyControl" %>
    <h3>This is my content</h3>

    <asp:Button ID="btnTest" runat="server" Text="click me!" /> 

    .Ascx code

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace SharePointWebPart1
    {
        public partial class MyControl : System.Web.UI.UserControl
        {
            protected Button btnTest;

            protected void Page_Load(object sender, EventArgs e)
            {
                Response.Write("Hello");

                this.btnTest.Click += new EventHandler(btnTest_Click);
            }

            void btnTest_Click(object sender, EventArgs e)
            {
                Response.Write("Great job!");
            }
        }
    }

     

     

     

More Posts