1

Hi I have a problem here with me I wrote this code below which just copies SPItems from one list to another another list the problem is it executes twice, when it copies items it creates two record instead of one.

using (SPWeb spWeb = SPContext.Current.Site.OpenWeb()) {

            SPListItemCollection LearningCalendar_items = spWeb.Lists["Learning Calendar345"].Items;

            UserProfileManager profile = new UserProfileManager();
            UserProfile name = profile.GetUserProfile(SPContext.Current.Web.CurrentUser.LoginName);
            string userName = name.DisplayName;


            foreach (SPListItem LearningCalendar_Item in LearningCalendar_items)
            {

                if (string.Compare(LearningCalendar_Item["ID"].ToString(), this.Page.Request.QueryString["myID"], false) == 0)
                {
                    spWeb.AllowUnsafeUpdates = true;
                    SPListItemCollection PersonalCalendar_items = spWeb.Lists["PersonalCalendar2"].Items;


                    SPListItem NewPersonaCCalendar_Item = PersonalCalendar_items.Add();


                    NewPersonaCCalendar_Item["EventOwner"] = SPContext.Current.Web.CurrentUser;
                    NewPersonaCCalendar_Item["Title"] = LearningCalendar_Item["Title"];
                    NewPersonaCCalendar_Item["Location"] = LearningCalendar_Item["Location"];
                    NewPersonaCCalendar_Item["Start Time"] = LearningCalendar_Item["Start Time"];
                    NewPersonaCCalendar_Item["End Time"] = LearningCalendar_Item["End Time"];
                    NewPersonaCCalendar_Item["Description"] = LearningCalendar_Item["Description"];
                    NewPersonaCCalendar_Item["All Day Event"] = LearningCalendar_Item["All Day Event"];
                    NewPersonaCCalendar_Item["Recurrence"] = LearningCalendar_Item["Recurrence"];
                    NewPersonaCCalendar_Item["Presenter"] = LearningCalendar_Item["Presenter"];
                    NewPersonaCCalendar_Item["OldID"] = LearningCalendar_Item["OldID"];
                    NewPersonaCCalendar_Item["Is External Presenter"] = LearningCalendar_Item["Is External Presenter"];
                    NewPersonaCCalendar_Item["External Presenter"] = LearningCalendar_Item["External Presenter"];
                    NewPersonaCCalendar_Item["Learning Journey Related"] = LearningCalendar_Item["Learning Journey Related"];
                    NewPersonaCCalendar_Item["Mandatory Learning"] = LearningCalendar_Item["Mandatory Learning"];
                    NewPersonaCCalendar_Item["Course Hours"] = LearningCalendar_Item["Course Hours"];
                    //NewPersonaCCalendar_Item["Course Attendees"] = name.DisplayName;
                    //NewPersonaCCalendar_Item["Attendants"] = name.DisplayName;
                    //NewPersonaCCalendar_Item["Learning Attendees"] = name.DisplayName;
                    NewPersonaCCalendar_Item.Update();


                    lblListID.Text = LearningCalendar_Item.ID.ToString();
                    lblTitle.Text = NewPersonaCCalendar_Item["Title"].ToString();
                    lblLocation.Text = NewPersonaCCalendar_Item["Location"].ToString();
                    lblDescription.Text = NewPersonaCCalendar_Item["Description"].ToString();
                    lblStartDate.Text = NewPersonaCCalendar_Item["Start Time"].ToString();
                    lblEndDate.Text = NewPersonaCCalendar_Item["End Time"].ToString();
                    HyperLinkOutlook.NavigateUrl = "/../_vti_bin/owssvr.dll?CS=109&Cmd=Display&List=52212766-2871-499B-AF8A-E3ADD9844090&CacheControl=1&ID=" + LearningCalendar_Item.ID.ToString() + "&Using=event.ics";
                    HyperLink.NavigateUrl = "/../pages/Learning-Calendar.aspx";



                    //Update Attendees

                    Microsoft.SharePoint.SPList LearningCal = default(Microsoft.SharePoint.SPList);
                    LearningCal = spWeb.Lists["Learning Calendar"];
                    SPListItemCollection LearningCalCollection = default(SPListItemCollection);
                    LearningCalCollection = LearningCal.Items;
                    foreach (SPListItem LearningItem in LearningCalCollection)
                    {
                        if (LearningItem["ID"] == LearningItem["OldID"])
                        {
                            try
                            {
                                string strUserColl = LearningItem["Learning Attendees"].ToString();
                                SPFieldUserValueCollection Attendees = new SPFieldUserValueCollection(SPContext.Current.Site.OpenWeb(), strUserColl);
                                var NewAttendees = new SPFieldUserValueCollection();
                                foreach (SPFieldUserValue CurrentAttendee in Attendees)
                                {
                                    NewAttendees.Add(CurrentAttendee);
                                }
                                NewAttendees.Add(new SPFieldUserValue(SPContext.Current.Web, SPContext.Current.Web.CurrentUser.UserId.ToString()));
                                LearningItem["Learning Attendees"] = NewAttendees;
                            }
                            catch (Exception ex)
                            {
                                LearningItem["Learning Attendees"] = SPContext.Current.Web.CurrentUser;
                            }
                            LearningItem.Update();
                        }
                    }
                    spWeb.AllowUnsafeUpdates = false;
                }
            }
1

Where is your code ? (in wich methode ?)
If your code is in Page_load or createChildControl it's normal. Sometimes SharePoint load twice the page, so your code will be executed twice.

  • 1
    Be careful with your code, Some best practices are not respected (like : SPList.Items or using multi instance of the same SPWeb ( re-instanciate web from SPContext, use the web of SPContext, re-instanciate Web of SPcontext.Current.Site .....). Your code will work but it could contains memory leak – Nico Jan 27 '17 at 14:39
  • My code is on Page_Load () which method should I use? – Ed Charles Diesel Jan 30 '17 at 9:14
  • 1
    It depend on what you want to do. With your logic every time you Load your page you will create a new item. It's not recommended to create item in a Get (protocol http). You can use a simple asp button to force your item creation on a POST (protocol http). Or you can use List Item Event Receivers (On ItemAdded). Or you can use a SharePoint Workflow to copy you item (On item Added) – Nico Jan 30 '17 at 10:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.