enter code hereI'm really new in SP World.

I have developed 2 Visual Web Parts which has few TextBox and a Button. What I want to do is onClick event to check if the Values in Text Box fields are ok(format is ok and make sure values of certain TextBox don't already exist in my SharePoint List), add new Item to my specific SPList with data from TextBoxes on the current site and create a new site. If Values in Text Box fields are not ok, I just go return from onClick event. This works ok on SharePoint 2010.

When I upload to SharePointOnline I have unpredictable behavior.

Fist web part adds listItem twice to my list and creates once the web site, when I deploy solution and test it for the first time or if I don't use for a long time. After that it works ok.

Secound web part adds listItem to specific List and again check are the values ok(code at the beginning in my onClick event ), because it's not ok never creates a site and never finish onClick event handler(goes return). It work like this every time.

 protected void ButtonSave_Click(object sender, EventArgs e) 
        SPSite site;
        SPWeb web;
        SPWebTemplate myTemplate;
        Boolean flag;
        String accountName;
        String accountUrl;
        SPWeb accountSite;

        site = SPContext.Current.Site;
        myTemplate = this.GetAccountWebTemplate(site);
        web = SPContext.Current.Web;
        flag = this.ItemAddToAccountList();
        if (!flag)
        accountName = TextBoxName.Text;
        accountUrl = accountName.Replace(" ", "");
        accountSite = web.Webs.Add(accountUrl, accountName, String.Empty, 1033, myTemplate, false, false);
        accountSite.Navigation.UseShared = false;
        if (!String.IsNullOrEmpty(TextBoxCrm.Text))
            this.CreateCrmField(accountSite, descriptionCrmLink, TextBoxCrm.Text);

private Boolean ItemAddToAccountList() 
        Boolean flag;
        SPList myAccountList;
        SPListItem listItem;
        SPFieldUrlValue crmPage;
        SPFieldUrlValue accountSiteUrlField;
        flag = this.VerifyFields();
        if (!flag)
            return false;
        SPContext.Current.Web.AllowUnsafeUpdates = true;
        myAccountList = SPContext.Current.Web.Lists[accountList];
        listItem = myAccountList.AddItem();
        listItem[accountNameFieldTitle] = TextBoxName.Text;
        listItem[columnStatus] = DropDownStatus.SelectedValue;
        foreach (SPUser user in SPContext.Current.Web.AllUsers)
            if (user.Name.Equals(TextBoxOwner.Text, StringComparison.InvariantCultureIgnoreCase) ||
                listItem[columnOwner] = user;
        if (!String.IsNullOrEmpty(TextBoxCrm.Text))
            listItem[accountLinkCrmFieldTitle] = TextBoxCrm.Text;
            crmPage = new SPFieldUrlValue();
            crmPage.Description = TextBoxName.Text;
            crmPage.Url = TextBoxCrm.Text;
            listItem[accountPageCrmFieldTitle] = crmPage;
        if (!String.IsNullOrEmpty(TextAreaNotes.Text))
            listItem[columnNotes] = TextAreaNotes;
        accountSiteUrlField = new SPFieldUrlValue();
        accountSiteUrlField.Description = TextBoxName.Text ;
        accountSiteUrlField.Url =SPContext.Current.Web.Url + "/" + TextBoxName.Text.Replace(" ", "");
        listItem[accountUrlFieldTitle] = accountSiteUrlField;
        SPContext.Current.Web.AllowUnsafeUpdates = false;
        return true;
  • Hi,Can you please provide the code you using for your onclick event. This will help us to advice you on what might be happening. – Renzo Jun 3 '13 at 14:03
  • Hi sunicha, thanks for adding your code, overall I am not sure about why that behavior. If I had to guess id say you have a double postback with the same parameters and the reason why is only the item the one that gets created 2 times and not the web is because you cannot have two webs with the same url. Might want to do some logging to see it this is the case. – Renzo Jun 4 '13 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.