0

I have created a custom list using custom content type. on feature activation, adding items in the list.

but I'm facing error

Column 'LeaveStart' does not exist. It may have been deleted by another user.

I have check columns. they are available in the list

I'm getting error on this line

leaveItem["LeaveStart"] = leaveStart;

Here is my whole code


public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPSite site = properties.Feature.Parent as SPSite)
            {
                using (SPWeb web = site.OpenWeb())
                {
                    var leaveItemContentType = site.RootWeb.ContentTypes[leaveContentTypeName];
                    var leaveItemUsages = SPContentTypeUsage.GetUsages(leaveItemContentType);

                    var leaveEventContentType = site.RootWeb.ContentTypes[leavesCalenderContentTypeName];
                    var leaveEventUsages = SPContentTypeUsage.GetUsages(leaveEventContentType);

                    foreach (var leaveEventUsage in leaveEventUsages)
                    {
                        if (leaveEventUsage.IsUrlToList)
                        {
                            string url = leaveEventUsage.Url;

                            int lengthOfWebUrl = url.LastIndexOf("/Lists", System.StringComparison.Ordinal);

                            string webUrlForList = url.Substring(0, lengthOfWebUrl);

                            SPWeb webWithList = site.OpenWeb(webUrlForList);

                            SPList leaveEventList = webWithList.GetList(url);

                            SPQuery leavesEvantQuery = new SPQuery();
                            leavesEvantQuery.Query = "";
                            SPListItemCollection leaveEvents = leaveEventList.GetItems(leavesEvantQuery);
                            foreach (SPItem leaveEvent in leaveEvents)
                            {

                                var title = leaveEvent["Title"];
                                var employeeOnLeave = leaveEvent["EmployeeOnLeave"];
                                var departmentTitle = leaveEvent["DepartmentTitle"];
                                var leaveStart = leaveEvent["EventDate"];
                                var leaveEnd = leaveEvent["EndDate"];
                                var itemIdentifier = leaveEvent["ID"];

                                foreach (var leaveItemUsage in leaveItemUsages)
                                {

                                    if (leaveItemUsage.IsUrlToList)
                                    {
                                        string leaveItemUrl = leaveItemUsage.Url;

                                        int leaveItemLengthOfWebUrl = url.LastIndexOf("/Lists", System.StringComparison.Ordinal);

                                        string leaveItemWebUrlForList = url.Substring(0, lengthOfWebUrl);

                                        SPWeb leaveItemWebWithList = site.OpenWeb(webUrlForList);
                                        //web.AllowUnsafeUpdates = true;
                                        SPList leaveItemList = webWithList.GetList(url);

                                        SPItem leaveItem = leaveItemList.AddItem();

                                        leaveItem["Title"] = title;
                                        leaveItem["EmployeeOnLeave"] = employeeOnLeave;
                                        leaveItem["DepartmentTitle"] = departmentTitle;

                                         leaveItem["LeaveStart"] = leaveStart;

                                        leaveItem["LeaveEnd"] = leaveEnd;
                                        leaveItem["ItemIdentifier"] = itemIdentifier;
                                        web.AllowUnsafeUpdates = true;
                                        leaveItem.Update();
                                    }
                                }

                            }



                        }
                    }
                }
            }
        }
2
  • is this column LeaveStart at your content type or your list ? Sep 2, 2016 at 14:14
  • Column is available in content type add I'm using that content type in list Sep 2, 2016 at 14:39

1 Answer 1

1

Make sure that you get the correct list (item list) and it's not the event list at this line

SPList leaveItemList = webWithList.GetList(url);

I noted that the previous fields EmployeeOnLeave and DepartmentTitle are already created at both lists so the code raises the error when hitting at LeaveStart that actually not exist at event list but it exists at item list as you checked, so I think the list name has been set to event list not item list

Also, Make sure that leaveItemContentType at var leaveItemUsages = SPContentTypeUsage.GetUsages(leaveItemContentType); have the column LeaveStart and is the default content type.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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