Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Logging information from one SPlist to another

$
0
0

I am trying to create a list that takes the "Finished" items under the Status column of a list and copies the Title and "assigned to" details and adds these information into a new list. I came up with this code. But I keep getting these errors. I don't know why? It is a Feature in a SharePoint Empty Project

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;

namespace LogFeature.Features.Feature1
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("b07b7618-f139-464e-a15d-79e4f91b700d")]
    public class Feature1EventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite oSite = properties.Feature.Parent as SPSite;
            SPWeb site = oSite.RootWeb;
            var list = site.Lists.TryGetList("Completed Task Log");
            if (list == null)
            {
                var listG = site.Lists.Add("Completed Task Log", "Last two weeks of completed tasks", SPListTemplateType.GenericList);
                list = site.Lists[listG];
                list.Fields.Add("Task", SPFieldType.Text, true);
                list.Fields.Add("Completed By", SPFieldType.Text, true);
                list.Fields.Add("Date", SPFieldType.DateTime, true);
                list.Update();
            }
            var newitem = list.Items.Add();
            var list2 = site.Lists["Today's Agenda"];
            SPListItemCollection lic = list2.GetItems("Status", "Assigned%5Fx0020%5FTo", "Title");
            for (int i = 0; i < lic.Count; i++)
            {
                SPListItem it = lic.GetItemById(i);
                if ((string)it["Status"] == "Finished")
                {
                    newitem["Task"] = (string)it["Title"];
                    //newitem["Completed By"] = (string)i["Assigned To"];
                    newitem["Date"] = DateTime.Now;
                } it.Delete();list.Update();
            }
            //SPListItemCollection lic1 = list.Items;
            //for (int i = 0; i < lic1.Count; i++)
            //{
            //    SPListItem ite = list.GetItemByIdSelectedFields(i, "Date");
            //    TimeSpan elapsed = DateTime.Now.Subtract((DateTime)ite["Date"]);
            //    double days = elapsed.TotalDays;
            //    if (days > 14)
            //    {
            //        ite.Delete();
            //    }
            //}
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite oSite = properties.Feature.Parent as SPSite;
            SPWeb site = oSite.RootWeb;
            var list = site.Lists.TryGetList("Completed Task Log");
            if (list != null)
            {
                list.Delete();
            }
        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}


Error1 Error occurred in deployment step 'Activate Features': Value does not fall within the expected range.
00LogFeature


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>