Sharepoint 2010 EventReceiver copying from one list to another – An item is being updated.

Posted: February 3, 2012 in web
    public class ERApproveRequest : SPItemEventReceiver
    {
       /// <summary>
       /// An item is being updated.
       /// </summary>
       /// 
        
        string list_Translator = "Translators";
        string list_Languages = "Languages";
        string list_title = "Change Request";

       //translator fields
        string transTitle = "Title";
        string transLookup = "Languages";
        string transContact = "Contact";
        string transEmail = "E-mail";
        string transAvailability = "Availability";
        //string transActive = "Active";

        //request field
        string reqTitle = "Name";
        string reqContact = "Contact Information";
        string reqEmail = "E-mail";
        string reqAvailability = "Availability";
        string reqApproved = "Approved";
        string reqTranslatorInfo = "Translator Information";



       public override void ItemUpdating(SPItemEventProperties properties)
       {
           //if list is Change Request 
           if (properties.ListTitle == list_title)
           {
               //get previous Approve value
               string prevApprove = properties.ListItem[reqApproved].ToString();
               //get current Approve value
               string currApprove = properties.AfterProperties[properties.ListItem.Fields[reqApproved].InternalName].ToString();

               // if on change and Approved is checked and previously Approve is unchecked
               if (currApprove == "True" && prevApprove == "False")
               {
                   //check if Translator name already exists
                   string name = properties.AfterProperties[properties.ListItem.Fields[reqTitle].InternalName].ToString();
                   if (checkName(name, properties))
                   {
                       properties.Status = SPEventReceiverStatus.CancelWithError;
                       properties.ErrorMessage = name + " already exists on Translator List.";
                       return;
                   }
                   else
                   {
                       //get all languages from List Languages
                       SPSite oSiteCollection = new SPSite(properties.WebUrl);
                       SPList oList = oSiteCollection.AllWebs[properties.RelativeWebUrl].Lists[list_Languages];
                       SPQuery oQuery = new SPQuery();
                       SPListItemCollection collListItems = oList.GetItems();
                       
                       //get a List Object (id,language) for matched languages
                       List<Lang> Langs = ParseLang(collListItems, properties.AfterProperties[properties.ListItem.Fields[reqTranslatorInfo].InternalName].ToString());

                       // add approved request to Translator List
                       AddTranslatorItem(properties, Langs);
                       oSiteCollection.Dispose();
                   }

               }
           }

           base.ItemUpdating(properties);
       }
       private bool checkName(string name, SPItemEventProperties properties)
       {
           SPSite oSiteCollection = new SPSite(properties.WebUrl);
           SPList oList = oSiteCollection.AllWebs[properties.RelativeWebUrl].Lists[list_Translator];
           
            SPQuery oQuery = new SPQuery();
            oQuery.Query = "<Where><Eq><FieldRef Name='"+transTitle+"'/>" +
                "<Value Type='Text'>"+name+"</Value></Eq></Where>";
            SPListItemCollection collListItems = oList.GetItems(oQuery);
            oSiteCollection.Dispose();
            if (collListItems.Count > 0)
                return true;
           return false;
       }
       private List<Lang> ParseLang(SPListItemCollection collListItems, string transInfo)
       {
           List<Lang> Langs = new List<Lang>();
           foreach (SPListItem oListItem in collListItems)
           {
               Lang lang = new Lang();
               if (transInfo.ToUpper().IndexOf(oListItem.Title.ToUpper()) != -1)  //found it
               {
                   lang.ID = oListItem.ID;
                   lang.Title = oListItem.Title;
                   Langs.Add(lang);
               }
           }
           return Langs;
       }
       private void AddTranslatorItem(SPItemEventProperties properties, List<Lang> Langs)
       {
           SPSite oSiteCollection = new SPSite(properties.WebUrl);
           using (SPWeb web = oSiteCollection.AllWebs[properties.RelativeWebUrl])
           {
               SPList list = web.Lists[list_Translator];
               SPList listLang = web.Lists[list_Languages];
               SPListItem item = list.Items.Add();
               item[transTitle] = properties.AfterProperties[properties.ListItem.Fields[reqTitle].InternalName].ToString();
               item[transContact] = properties.AfterProperties[properties.ListItem.Fields[reqContact].InternalName].ToString();
               item[transAvailability] = properties.AfterProperties[properties.ListItem.Fields[reqAvailability].InternalName].ToString();
               item[transEmail] = properties.AfterProperties[properties.ListItem.Fields[reqEmail].InternalName].ToString();

               //adding lookup
               SPFieldLookup field = item.Fields.GetField(transLookup) as SPFieldLookup;

               SPFieldLookupValueCollection fieldValues = new SPFieldLookupValueCollection();
               foreach (Lang lang in Langs)
               {
                   SPFieldLookupValue firstLookupValue = new SPFieldLookupValue(lang.ID, "");
                   fieldValues.Add(firstLookupValue);
               }

               item[transLookup] = fieldValues;

               item.Update();
               oSiteCollection.Dispose();
               web.Dispose();
           }
           

       }

    }
    
    public class Lang
    {
        public int ID { get; set; }
        public string Title { get; set; }
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s