Solved

SharePoint Web Part Help

Posted on 2011-02-21
3
618 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:Isaac
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34943975
Line 90 should say:
btn_Submit.Click += new EventHandler(btn_Submit_Click);

Open in new window

Line 111 should probably be:
SPWeb newSite = mySite.Add(txt_siteUrl.Text, txt_siteName.Text, txt_siteDesc.Text, 1033, "STS#1", true, false);

Open in new window

0
 
LVL 5

Author Comment

by:Isaac
ID: 34944053
Thanks!
0
 
LVL 5

Author Closing Comment

by:Isaac
ID: 34944056
Excellent!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We had a requirement to extract data from a SharePoint 2010 Customer List into a CSV file and then place the CSV file into a directory on the network so that the file could be consumed by an AS400 system. I will share in Part 1 how to Extract the Da…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question