1

I am working on two SharePoint list:List 1 is overview of servers including: Server Name, Purpose, status etc.. List 2 is change log on each server, it got an lookup column that connects to Server Name in List 1. What I want to do is each time a new log is added/deleted/updated in List 2, it will automatically add/delete/update in List1, because there will be several logs for one server and we are not using Infopath for repeating section, I decide to use C# code and build a Event Receiver. But I only start with c# and have no idea how to write them at all. Now I only got this. Could anyone show me how the code would be like, I will be so grateful.

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace AppendListColumn.AppendColumn
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class AppendColumn : SPItemEventReceiver
    {
        /// <summary>
        /// An item is being added.
        /// </summary>
        public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);
        }

        /// <summary>
        /// An item is being updated.
        /// </summary>
        public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.ItemUpdating(properties);
        }

        /// <summary>
        /// An item is being deleted.
        /// </summary>
        public override void ItemDeleting(SPItemEventProperties properties)
        {
            base.ItemDeleting(properties);
        }


    }
}

Thanks a lot, Celia

1
  public override void ItemUpdating(SPItemEventProperties properties)
    {
        base.ItemUpdating(properties);
        sendItemTolist2(properties);
    }

 private void sendItemTolist2(SPItemEventProperties properties)
        {
 SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        SPWeb webPropertiesListItem = properties.Web;
                        using (SPSite oSite = webPropertiesListItem.Site)
                        {
                        //if you want list2 is on another sitecollection
                            using (SPWeb webRH = oSite.OpenWeb("Name Web"))
                            {
                                webRH.AllowUnsafeUpdates = true;
                                SPList EventList = webRH.Lists["List2"];
                                SPListItem EventListElmt = EventList.Items.Add();
                                EventListElmt["Title"] = _currentItem["Title"];
                                ...
                                EventListElmt.Update();
                            }

                            //if list2 is on the same site then just copy to list2


                                webPropertiesListItem.AllowUnsafeUpdates = true;
                                SPList EventList = webPropertiesListItem.Lists["List2"];
                                SPListItem EventListElmt = EventList.Items.Add();
                                EventListElmt["Title"] = _currentItem["Title"];
                                ...
                                EventListElmt.Update();
                        }
                    });
                    }
|improve this answer|||||

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.