Move List SharePoint WebPart

This example shows you how to move a list within an arbitrary number of webs in SharePoint.

[csharp]

[DefaultProperty(“Text”), XmlRoot(Namespace = “MoveList”), ToolboxData(“<{0}:WebPart1 runat=server></{0}:WebPart1>”)]
public class MoveList : WebPart
{
        public MoveList()
        {
                this._text = “”;
        }

        protected void btnGetWebs_Click(object sender, EventArgs e)
        {
                this.GetAvailableSubWebs();
        }

        protected override void CreateChildControls()
        {
                this.txtMessage = new Label();
                this.Controls.Add(this.txtMessage);
                this.txtList1 = new TextBox();
                this.Controls.Add(this.txtList1);
                this.txtList2 = new TextBox();
                this.Controls.Add(this.txtList2);
                this.txtWeb1 = new TextBox();
                this.Controls.Add(this.txtWeb1);
                this.txtWeb2 = new TextBox();
                this.Controls.Add(this.txtWeb2);
                this.btnGetLists = new Button();
                this.btnGetLists.Text = “Get Lists”;
                this.Controls.Add(this.btnGetLists);
                this.btnGetWebs = new Button();
                this.btnGetWebs.Text = “Get Webs”;
                this.Controls.Add(this.btnGetWebs);
                this.btnSubmit = new Button();
                this.btnSubmit.Text = “Move List”;
                this.Controls.Add(this.btnSubmit);
                this.ddlLists = new DropDownList();
                this.Controls.Add(this.ddlLists);
                this.ddlSourceWeb = new DropDownList();
                this.Controls.Add(this.ddlSourceWeb);
                this.ddlDestinationWeb = new DropDownList();
                this.Controls.Add(this.ddlDestinationWeb);
                this.chbIncludeDocs = new CheckBox();
                this.chbIncludeDocs.Text = “Include Data/Documents”;
                this.Controls.Add(this.chbIncludeDocs);
        }

        protected void GetAvailableSubWebs()
        {
                SPSite site1 = SPControl.GetContextSite(this.Context);
                this.ddlSourceWeb.Items.Clear();
                this.ddlDestinationWeb.Items.Clear();
                foreach (SPWeb web1 in site1.AllWebs)
                {
                        ListItem item1 = new ListItem(web1.ToString() + ” (” + web1.Url + “)”, web1.ServerRelativeUrl);
                        this.ddlSourceWeb.Items.Add(item1);
                        this.ddlDestinationWeb.Items.Add(item1);
                }
        }

        protected void GetAvailableWebs()
        {
                IEnumerator enumerator2;
                SPSite site1 = SPControl.GetContextSite(this.Context);
                SPGlobalAdmin admin1 = new SPGlobalAdmin();
                Uri uri1 = new Uri(site1.RootWeb.Url.ToString());
                SPVirtualServer server1 = admin1.OpenVirtualServer(uri1);
                try
                {
                        enumerator2 = server1.Sites.GetEnumerator();
                        while (enumerator2.MoveNext())
                        {
                                site1 = (SPSite)enumerator2.Current;
                                try
                                {
                                        foreach (SPWeb web1 in site1.AllWebs)
                                        {
                                                ListItem item1 = new ListItem(web1.ToString() + ” (” + web1.Url + “)”, web1.ServerRelativeUrl);
                                                this.ddlSourceWeb.Items.Add(item1);
                                                this.ddlDestinationWeb.Items.Add(item1);
                                        }
                                }
                                finally
                                {
                                        IEnumerator enumerator1;
                                        if (enumerator1 is IDisposable)
                                        {
                                                ((IDisposable)enumerator1).Dispose();
                                        }
                                }
                        }
                }
                finally
                {
                        if (enumerator2 is IDisposable)
                        {
                                ((IDisposable)enumerator2).Dispose();
                        }
                }
        }

        protected void GetLists(object Sender, EventArgs e)
        {
                SPSite site1 = new SPSite(“http://localhost“);
                SPGlobalAdmin admin1 = new SPGlobalAdmin();
                Uri uri1 = new Uri(site1.Url);
                this.txtMessage.Text = this.ddlSourceWeb.SelectedItem.Value.ToString();
                SPVirtualServer server1 = admin1.VirtualServers[0];
                site1 = server1.Sites[this.ddlSourceWeb.SelectedItem.Value.ToString()];
                SPWeb web1 = site1.AllWebs[this.ddlSourceWeb.SelectedItem.Value.ToString()];
                this.ddlLists.Items.Clear();
                foreach (SPList list1 in web1.Lists)
                {
                        ListItem item1 = new ListItem(list1.ToString(), list1.ToString());
                        this.ddlLists.Items.Add(item1);
                }
        }

        protected void MoveList(object sender, EventArgs e)
        {
                SPListTemplate template1;
                SPSite site1 = new SPSite(“http://localhost/” + this.ddlSourceWeb.SelectedItem.Value);
                SPWeb web2 = site1.AllWebs[this.ddlSourceWeb.SelectedItem.Value];
                SPList list2 = web2.Lists[this.ddlLists.SelectedItem.Value.ToString()];
                this.txtMessage.Text = “”;
                try
                {
                        list2.SaveAsTemplate(list2.Title.ToString(), list2.Title.ToString(), “test”, this.chbIncludeDocs.Checked);
                }
                catch (Exception exception1)
                {
                        ProjectData.SetProjectError(exception1);
                        this.txtMessage.Text = this.txtMessage.Text + “A Template with this name already exsists, using previous template as basis for new list</br>”;
                        ProjectData.ClearProjectError();
                }
                SPSite site2 = new SPSite(“http://localhost/” + this.ddlDestinationWeb.SelectedItem.Value);
                SPWeb web1 = site2.AllWebs[this.ddlDestinationWeb.SelectedItem.Value];
                SPListTemplateCollection collection1 = site1.GetCustomListTemplates(site1.AllWebs[this.ddlSourceWeb.SelectedItem.Value]);
                try
                {
                        template1 = collection1[list2.Title.ToString()];
                }
                catch (Exception exception2)
                {
                        ProjectData.SetProjectError(exception2);
                        this.txtMessage.Text = this.txtMessage.Text + “Error Getting Template </br>”;
                        ProjectData.ClearProjectError();
                }
                web1.Lists.Add(list2.Title, list2.Description, template1);
                this.txtMessage.Text = this.txtMessage.Text + “Template with name <strong>” + list2.Title.ToString() + “.stp</strong> may de deleted from the default list template gallery”;
        }

        protected override void RenderWebPart(HtmlTextWriter output)
        {
                output.Write(SPEncode.HtmlEncode(this.Text));
                this.txtMessage.RenderControl(output);
                output.Write(“<TABLE width=\”100%\” cellpadding=\”5\”>”);
                output.WriteLine(“<TR>”);
                output.WriteLine(“<TD>”);
                output.Write(“<strong>Step 1:</strong>”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“<TD>”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“</TR>”);
                output.WriteLine(“<TR>”);
                output.WriteLine(“<TD>”);
                output.Write(“Find Available Webs”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“<TD>”);
                this.btnGetWebs.RenderControl(output);
                output.WriteLine(“</TD>”);
                output.WriteLine(“</TR>”);
                output.WriteLine(“<TR>”);
                output.WriteLine(“<TD>”);
                output.Write(“<strong>Step 2:</strong>”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“<TD>”);
                output.Write(“”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“</TR>”);
                output.WriteLine(“<TR>”);
                output.WriteLine(“<TD>”);
                output.Write(“Select Source Web”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“<TD>”);
                this.ddlSourceWeb.RenderControl(output);
                output.WriteLine(“</TD>”);
                output.WriteLine(“</TR>”);
                output.WriteLine(“<TR>”);
                output.WriteLine(“<TD>”);
                output.Write(“<strong>Step 3:</strong>”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“<TD>”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“</TR>”);
                output.WriteLine(“<TR>”);
                output.WriteLine(“<TD>”);
                output.Write(“Get Lists On Source Web”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“<TD>”);
                this.btnGetLists.RenderControl(output);
                output.WriteLine(“</TD>”);
                output.WriteLine(“</TR>”);
                output.WriteLine(“<TR>”);
                output.WriteLine(“<TD>”);
                output.Write(“<strong>Step 4:</strong>”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“<TD>”);
                output.Write(“”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“</TR>”);
                output.WriteLine(“<TR>”);
                output.WriteLine(“<TD>”);
                output.Write(“Select Source List To Copy”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“<TD>”);
                this.ddlLists.RenderControl(output);
                output.WriteLine(“</TD>”);
                output.WriteLine(“</TR>”);
                output.WriteLine(“<TR>”);
                output.WriteLine(“<TD>”);
                output.Write(“<strong>Step 5:</strong>”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“<TD>”);
                output.Write(“”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“</TR>”);
                output.WriteLine(“<TR>”);
                output.WriteLine(“<TD>”);
                output.Write(“Select Destination Web Where the List Will Be Copied To”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“<TD>”);
                this.ddlDestinationWeb.RenderControl(output);
                output.WriteLine(“</TD>”);
                output.WriteLine(“</TR>”);
                output.WriteLine(“<TR>”);
                output.WriteLine(“<TD>”);
                output.Write(“<strong>Step 6:</strong>”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“<TD>”);
                output.Write(“”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“</TR>”);
                output.WriteLine(“<TR>”);
                output.WriteLine(“<TD>”);
                output.Write(“Should the Data/Documents Be Included in the Copy?”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“<TD>”);
                this.chbIncludeDocs.RenderControl(output);
                output.WriteLine(“</TD>”);
                output.WriteLine(“</TR>”);
                output.WriteLine(“<TR>”);
                output.WriteLine(“<TD>”);
                output.WriteLine(“</TD>”);
                output.WriteLine(“<TD>”);
                this.btnSubmit.RenderControl(output);
                output.WriteLine(“</TD>”);
                output.WriteLine(“</TR>”);
                output.WriteLine(“</TABLE>”);
        }
        protected virtual Button btnGetLists
        {
                get
                {
                        return this._btnGetLists;
                }
                [MethodImpl(MethodImplOptions.Synchronized)]
                set
                {
                        if (this._btnGetLists != null)
                        {
                                this._btnGetLists.Click -= new EventHandler(this.GetLists);
                        }
                        this._btnGetLists = value;
                        if (this._btnGetLists != null)
                        {
                                this._btnGetLists.Click += new EventHandler(this.GetLists);
                        }
                }
        }

        protected virtual Button btnGetWebs
        {
                get
                {
                        return this._btnGetWebs;
                }
                [MethodImpl(MethodImplOptions.Synchronized)]
                set
                {
                        if (this._btnGetWebs != null)
                        {
                                this._btnGetWebs.Click -= new EventHandler(this.btnGetWebs_Click);
                        }
                        this._btnGetWebs = value;
                        if (this._btnGetWebs != null)
                        {
                                this._btnGetWebs.Click += new EventHandler(this.btnGetWebs_Click);
                        }
                }
        }

        protected virtual Button btnSubmit
        {
                get
                {
                        return this._btnSubmit;
                }
                [MethodImpl(MethodImplOptions.Synchronized)]
                set
                {
                        if (this._btnSubmit != null)
                        {
                                this._btnSubmit.Click -= new EventHandler(this.MoveList);
                        }
                        this._btnSubmit = value;
                        if (this._btnSubmit != null)
                        {
                                this._btnSubmit.Click += new EventHandler(this.MoveList);
                        }
                }
        }

        protected virtual CheckBox chbIncludeDocs
        {
                get
                {
                        return this._chbIncludeDocs;
                }
                [MethodImpl(MethodImplOptions.Synchronized)]
                set
                {
                        if (this._chbIncludeDocs != null)
                        {
                        }
                        this._chbIncludeDocs = value;
                        if (this._chbIncludeDocs != null)
                        {
                        }
                }
        }

        protected virtual DropDownList ddlDestinationWeb
        {
                get
                {
                        return this._ddlDestinationWeb;
                }
                [MethodImpl(MethodImplOptions.Synchronized)]
                set
                {
                        if (this._ddlDestinationWeb != null)
                        {
                        }
                        this._ddlDestinationWeb = value;
                        if (this._ddlDestinationWeb != null)
                        {
                        }
                }
        }

        protected virtual DropDownList ddlLists
        {
                get
                {
                        return this._ddlLists;
                }
                [MethodImpl(MethodImplOptions.Synchronized)]
                set
                {
                        if (this._ddlLists != null)
                        {
                        }
                        this._ddlLists = value;
                        if (this._ddlLists != null)
                        {
                        }
                }
        }

        protected virtual DropDownList ddlSourceWeb
        {
                get
                {
                        return this._ddlSourceWeb;
                }
                [MethodImpl(MethodImplOptions.Synchronized)]
                set
                {
                        if (this._ddlSourceWeb != null)
                        {
                        }
                        this._ddlSourceWeb = value;
                        if (this._ddlSourceWeb != null)
                        {
                        }
                }
        }

        [Browsable(true), FriendlyName(“Text”), WebPartStorage(Storage.Personal), DefaultValue(“”), Category(“Miscellaneous”), Description(“Text Property”)]
        public string Text
        {
                get
                {
                        return this._text;
                }
                set
                {
                        this._text = value;
                }
        }

        protected virtual TextBox txtList1
        {
                get
                {
                        return this._txtList1;
                }
                [MethodImpl(MethodImplOptions.Synchronized)]
                set
                {
                        if (this._txtList1 != null)
                        {
                        }
                        this._txtList1 = value;
                        if (this._txtList1 != null)
                        {
                        }
                }
        }

        protected virtual TextBox txtList2
        {
                get
                {
                        return this._txtList2;
                }
                [MethodImpl(MethodImplOptions.Synchronized)]
                set
                {
                        if (this._txtList2 != null)
                        {
                        }
                        this._txtList2 = value;
                        if (this._txtList2 != null)
                        {
                        }
                }
        }

        protected virtual Label txtMessage
        {
                get
                {
                        return this._txtMessage;
                }
                [MethodImpl(MethodImplOptions.Synchronized)]
                set
                {
                        if (this._txtMessage != null)
                        {
                        }
                        this._txtMessage = value;
                        if (this._txtMessage != null)
                        {
                        }
                }
        }

        protected virtual TextBox txtWeb1
        {
                get
                {
                        return this._txtWeb1;
                }
                [MethodImpl(MethodImplOptions.Synchronized)]
                set
                {
                        if (this._txtWeb1 != null)
                        {
                        }
                        this._txtWeb1 = value;
                        if (this._txtWeb1 != null)
                        {
                        }
                }
        }

        protected virtual TextBox txtWeb2
        {
                get
                {
                        return this._txtWeb2;
                }
                [MethodImpl(MethodImplOptions.Synchronized)]
                set
                {
                        if (this._txtWeb2 != null)
                        {
                        }
                        this._txtWeb2 = value;
                        if (this._txtWeb2 != null)
                        {
                        }
                }
        }
        [AccessedThroughProperty(“btnGetLists”)]
        private Button _btnGetLists;
        [AccessedThroughProperty(“btnGetWebs”)]
        private Button _btnGetWebs;
        [AccessedThroughProperty(“btnSubmit”)]
        private Button _btnSubmit;
        [AccessedThroughProperty(“chbIncludeDocs”)]
        private CheckBox _chbIncludeDocs;
        [AccessedThroughProperty(“ddlDestinationWeb”)]
        private DropDownList _ddlDestinationWeb;
        [AccessedThroughProperty(“ddlLists”)]
        private DropDownList _ddlLists;
        [AccessedThroughProperty(“ddlSourceWeb”)]
        private DropDownList _ddlSourceWeb;
        private const string _defaultText = “”;
        private string _text;
        [AccessedThroughProperty(“txtList1”)]
        private TextBox _txtList1;
        [AccessedThroughProperty(“txtList2”)]
        private TextBox _txtList2;
        [AccessedThroughProperty(“txtMessage”)]
        private Label _txtMessage;
        [AccessedThroughProperty(“txtWeb1”)]
        private TextBox _txtWeb1;
        [AccessedThroughProperty(“txtWeb2”)]
        private TextBox _txtWeb2;

[/csharp]

Share

Leave a Reply

Your email address will not be published. Required fields are marked *