Link to home
Start Free TrialLog in
Avatar of Isaac
IsaacFlag for United States of America

asked on

SharePoint Web Part Help

Please see below code below.

I am trying to create a webpart that allows a person to create sub-sites using a existing site template that's provided in a drop down list.

I get the following error when I run the code:

Error      2      The event 'System.Web.UI.WebControls.Button.Click' can only appear on the left hand side of += or -=      line 90

Error      3      The best overloaded method match for 'Microsoft.SharePoint.SPWebCollection.Add(string, string, string, uint, string, bool, bool)' has some invalid arguments         line 111

Error      4      Argument '1': cannot convert from 'System.Web.UI.WebControls.TextBox' to 'string'    line 111

Error      5      Argument '2': cannot convert from 'System.Web.UI.WebControls.TextBox' to 'string'    line 111

Error      6      Argument '3': cannot convert from 'System.Web.UI.WebControls.TextBox' to 'string'   line 111
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace CreateSolutionSiteWebPart
{
    [Guid("0b2ced02-2687-4c50-9b57-f9b2ea5f31cc")]
    public class CreateSolutionSiteWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        // declare all your controls here so they don't pass out of scope
        Label lbl_siteName = null;
        Label lbl_siteDesc = null;
        Label lbl_siteUrl = null;
        Label lbl_drpDwnList = null;
        TextBox txt_siteName = null;
        TextBox txt_siteDesc = null;
        TextBox txt_siteUrl = null;
        DropDownList drp_siteTempLates = null;
        Button btn_Submit = null;
        Label lbl_uPermissions = null;
        CheckBox txt_uPermissions = null;

        public CreateSolutionSiteWebPart()
        {
        }

        // configure and populate all your controls here
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            // TODO: add custom rendering code here.
            // Label label = new Label();
            // label.Text = "Hello World";
            // this.Controls.Add(label);
            lbl_siteName = new Label();
            lbl_siteDesc = new Label();
            lbl_siteUrl = new Label();
            lbl_drpDwnList = new Label();

            txt_siteName = new TextBox();
            txt_siteDesc = new TextBox();
            txt_siteUrl = new TextBox();
            drp_siteTempLates = new DropDownList();

            lbl_siteName.Text = "Name: ";
            Controls.Add(lbl_siteName);

            txt_siteName.Width = new Unit(295, UnitType.Pixel);
            Controls.Add(txt_siteName);

            lbl_siteDesc.Text = "Description: ";
            Controls.Add(lbl_siteDesc);

            txt_siteDesc.Width = new Unit(295, UnitType.Pixel);
            Controls.Add(txt_siteDesc);

            lbl_siteUrl.Text = "Url: ";
            Controls.Add(lbl_siteUrl);

            txt_siteUrl.Width = new Unit(295, UnitType.Pixel);
            Controls.Add(txt_siteUrl);

            lbl_drpDwnList.Text = "Make a Selection";
            Controls.Add(lbl_drpDwnList);

            SPSite site;
            SPWebTemplateCollection coll;
            site = SPContext.Current.Site;
            coll = site.GetWebTemplates(SPContext.Current.RegionalSettings.LocaleId);
            for (int i = 0; i < coll.Count; i++)
            {
                ListItem li = new ListItem();
                li.Text = coll[i].Title;
                li.Value = coll[i].Name;
                drp_siteTempLates.Items.Add(li);
            }


            btn_Submit = new Button();
            btn_Submit.Width = new Unit(50, UnitType.Pixel);
            btn_Submit.Text = "Submit";
            btn_Submit.Click += new EventHandler(btn_Submit.Click);  <- Line 90
            Controls.Add(btn_Submit);

        }

        void OnLoad(object sender, EventArgs e)
        {
            EnsureChildControls();
        }

        void btn_Submit_Click(object sender, EventArgs e)
        {
            Guid siteID = SPContext.Current.Site.ID;
            Guid webSiteID = SPContext.Current.Web.ID;

            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite site = new SPSite(siteID))
                {
                    SPWeb myWeb = site.OpenWeb();
                    SPWebCollection mySite = myWeb.Webs;
                    SPWeb newSite = mySite.Add(txt_siteUrl, txt_siteName, txt_siteDesc, 1033, "STS#1", true, false);  <-- Line 111
                }

            });
        }
        // remember to have functions to populate the dropdown
        // and create the site

        // you can use this function to get the values of controls 
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

        }

        // use this function to layout your controls
        protected void Render(HtmlTextWriter writer)
        {
            // lbltest.RenderControl(writer);   put down the control
            // writer.Write(@""&nbsp;");        add a space at the end
            writer.Write(@"<Table width='35%' align='center'><tr><td>");
            lbl_siteName.RenderControl(writer);
            writer.Write(@"</td><td>");
            txt_siteName.RenderControl(writer);
            writer.Write(@"</td></tr><tr><td>");
            lbl_siteDesc.RenderControl(writer);
            writer.Write(@"</td><td>");
            txt_siteDesc.RenderControl(writer);
            writer.Write(@"</td></tr><tr><td>");
            lbl_siteUrl.RenderControl(writer);
            writer.Write(@"</td><td>");
            txt_siteUrl.RenderControl(writer);
            writer.Write(@"</td></tr><tr><td colspan='2'>");
            btn_Submit.RenderControl(writer);
            writer.Write(@"</td></tr></table>");
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Isaac

ASKER

Thanks!
Avatar of Isaac

ASKER

Excellent!