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;
}
}