Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, January 28, 2013

Live Task How to get User Defined Fields within a Custom List Type using SharePoint 2010 Event Reiever


public override void ItemUpdating(SPItemEventProperties properties)
       {
           ArrayList ArrColumnNames = new ArrayList();
           ArrayList ArrOldValues = new ArrayList();
           ArrayList ArrNewValues = new ArrayList();

           SPListItem item = properties.ListItem;
           for (int i = 0; i <= properties.List.Fields.Count - 1; i++)
           {
               if (!properties.List.Fields[i].Hidden && !properties.List.Fields[i].ReadOnlyField && properties.List.Fields[i].Type != SPFieldType.Attachments && properties.List.Fields[i].Title != "Content Type")
               {
                   if (item[properties.List.Fields[i].StaticName] != null)
                   {
                       string strColumnName= properties.List.Fields[i].StaticName.ToString();
                       if (item[strColumnName] != null)
                       {
                           string strColumnType = properties.List.Fields[i].Type.ToString();
                           if (strColumnType == "Text")
                           {
                               ArrColumnNames.Add(properties.List.Fields[i].Title);
                               ArrOldValues.Add(item[strColumnName]).ToString();
                               ArrNewValues.Add(properties.AfterProperties[strColumnName].ToString());
                           }
                           if (strColumnType == "Choice")
                           {
                               ArrColumnNames.Add(properties.List.Fields[i].Title);
                               ArrOldValues.Add(item[strColumnName]).ToString();
                               ArrNewValues.Add(properties.AfterProperties[strColumnName].ToString());
                           }
                           if (strColumnType == "Lookup")
                           {
                               ArrColumnNames.Add(properties.List.Fields[i].Title);
                               SPFieldLookupValue value=new SPFieldLookupValue(item[strColumnName].ToString());
                               ArrOldValues.Add(value.LookupValue);
                               string srNewValueLookUp = (string)properties.AfterProperties[strColumnName].ToString();
                               SPFieldLookup lookup = (SPFieldLookup)properties.List.Fields[strColumnName];
                               Int32 singlelookupItemID = Convert.ToInt32(properties.AfterProperties[strColumnName]);
                               string strNewLookUpValue = properties.Web.Lists[new Guid(lookup.LookupList)].GetItemById(singlelookupItemID)[lookup.LookupField].ToString();
                               ArrNewValues.Add(strNewLookUpValue);
                           }

                           if (strColumnType == "DateTime")
                           {
                               ArrColumnNames.Add(properties.List.Fields[i].Title);
                               ArrOldValues.Add(Convert.ToDateTime(item[strColumnName].ToString()).ToShortDateString());
                               ArrNewValues.Add(Convert.ToDateTime(properties.AfterProperties[strColumnName].ToString()).ToShortDateString());
                           }
                       }
                   }
               }
           }
          
           string strColumnHeaderTitle = "";
           string strDynamicContent = "";
           string strNewModifiedData = "";
           string strOldModifiedData = "";
           string strCurrentUserName = properties.UserDisplayName;
           DateTime strCurrentDate = Convert.ToDateTime(DateTime.Today);
           ArrayList ArrModifiedColumnValues = new ArrayList();
           ArrayList ArrModifiedDynamicData = new ArrayList();
           ArrayList ArrNonModifiedColumnValues = new ArrayList();
           ArrayList ArrNonModifiedDynamicData = new ArrayList();
           for (int j = 0; j < ArrOldValues.Count; j++)
           {
               strColumnHeaderTitle += ArrColumnNames[j].ToString()+"||";
               strDynamicContent += ArrOldValues[j].ToString()+"||";
               string strOldValue = ArrOldValues[j].ToString();
               string strNewValue = ArrNewValues[j].ToString();
              
               ArrNonModifiedColumnValues.Add(ArrColumnNames[j].ToString());
               ArrNonModifiedDynamicData.Add(strOldValue);

               if (strOldValue != strNewValue)
               {
                   ArrModifiedColumnValues.Add(ArrColumnNames[j].ToString());
                   ArrModifiedDynamicData.Add(strNewValue.ToString());
               }
           }
           strNewModifiedData += "<Table width='100%' celpadding='0' cellspacing='0'>";
           strNewModifiedData += "<Tr>";
           strNewModifiedData += "<Td Colspan='2' bgcolor='Red'>";
           strNewModifiedData += "</Td>";
           strNewModifiedData += "</Tr>";
           strNewModifiedData += "<Tr>";
           strNewModifiedData += "<Td Colspan='2' Align='Center'>";
           strNewModifiedData += "<Strong>";
           strNewModifiedData += "New Data";
           strNewModifiedData += "</Strong>";
           strNewModifiedData += "</Td>";
           strNewModifiedData += "</Tr>";
           strNewModifiedData += "<Tr>";
           strNewModifiedData += "<Td Colspan='2' bgcolor='Red'>";
           strNewModifiedData += "</Td>";
           strNewModifiedData += "</Tr>";
           for (int l = 0; l < ArrModifiedColumnValues.Count; l++)
           {
               strNewModifiedData += "<Tr>";
               strNewModifiedData += "<Td>";
               strNewModifiedData += ArrModifiedColumnValues[l].ToString();
               strNewModifiedData += "</Td>";
               strNewModifiedData += "<Td>";
               strNewModifiedData += ArrModifiedDynamicData[l].ToString();
               strNewModifiedData += "</Td>";
               strNewModifiedData += "</Tr>";
           }
           strNewModifiedData += "<Tr>";
           strNewModifiedData += "<Td>";
           strNewModifiedData += "Modified By";
           strNewModifiedData += "</Td>";
           strNewModifiedData += "<Td>";
           strNewModifiedData += strCurrentUserName;
           strNewModifiedData += "</Td>";
           strNewModifiedData += "</Tr>";
           strNewModifiedData += "<Tr>";
           strNewModifiedData += "<Td>";
           strNewModifiedData += "Modified Date";
           strNewModifiedData += "</Td>";
           strNewModifiedData += "<Td>";
           strNewModifiedData += strCurrentDate.ToShortDateString();
           strNewModifiedData += "</Td>";
           strNewModifiedData += "</Tr>";
           strNewModifiedData += "<Tr>";
           strNewModifiedData += "<Td>";
           strNewModifiedData += "List Name";
           strNewModifiedData += "</Td>";
           strNewModifiedData += "<Td>";
           strNewModifiedData += properties.List.Title.ToString();
           strNewModifiedData += "</Td>";
           strNewModifiedData += "</Tr>";
           strNewModifiedData += "<Tr>";
           strNewModifiedData += "<Td>";
           strNewModifiedData += "Item ID";
           strNewModifiedData += "</Td>";
           strNewModifiedData += "<Td>";
           strNewModifiedData += properties.ListItem.ID.ToString();
           strNewModifiedData += "</Td>";
           strNewModifiedData += "</Tr>";
           strNewModifiedData += "<Tr>";
           strNewModifiedData += "<Td Colspan='2' bgcolor='Red'>";
           strNewModifiedData += "</Td>";
           strNewModifiedData += "</Tr>";
           strNewModifiedData += "</Table>";
           strNewModifiedData += "<Br />";
           strOldModifiedData += "<Table width='100%' celpadding='0' cellspacing='0'>";
           strOldModifiedData += "<Tr>";
           strOldModifiedData += "<Td Colspan='2' bgcolor='Red'>";
           strOldModifiedData += "</Td>";
           strOldModifiedData += "</Tr>";
           strOldModifiedData += "<Tr>";
           strOldModifiedData += "<Td Colspan='2' Align='Center'>";
           strOldModifiedData += "<Strong>";
           strOldModifiedData += "Old Data";
           strOldModifiedData += "</Strong>";
           strOldModifiedData += "</Td>";
           strOldModifiedData += "</Tr>";
           strOldModifiedData += "<Tr>";
           strOldModifiedData += "<Td Colspan='2' bgcolor='Red'>";
           strOldModifiedData += "</Td>";
           strOldModifiedData += "</Tr>";
           for (int m = 0; m < ArrNonModifiedColumnValues.Count; m++)
           {
               strOldModifiedData += "<Tr>";
               strOldModifiedData += "<Td>";
               strOldModifiedData += ArrNonModifiedColumnValues[m].ToString();
               strOldModifiedData += "</Td>";
               strOldModifiedData += "<Td>";
               strOldModifiedData += ArrNonModifiedDynamicData[m].ToString();
               strOldModifiedData += "</Td>";
               strOldModifiedData += "</Tr>";
           }
           strOldModifiedData += "<Tr>";
           strOldModifiedData += "<Td>";
           strOldModifiedData += "Modified By";
           strOldModifiedData += "</Td>";
           strOldModifiedData += "<Td>";
           strOldModifiedData += strCurrentUserName;
           strOldModifiedData += "</Td>";
           strOldModifiedData += "</Tr>";
           strOldModifiedData += "<Tr>";
           strOldModifiedData += "<Td>";
           strOldModifiedData += "Modified Date";
           strOldModifiedData += "</Td>";
           strOldModifiedData += "<Td>";
           strOldModifiedData += strCurrentDate.ToShortDateString();
           strOldModifiedData += "</Td>";
           strOldModifiedData += "</Tr>";
           strOldModifiedData += "<Tr>";
           strOldModifiedData += "<Td>";
           strOldModifiedData += "List Name";
           strOldModifiedData += "</Td>";
           strOldModifiedData += "<Td>";
           strOldModifiedData += properties.List.Title.ToString();
           strOldModifiedData += "</Td>";
           strOldModifiedData += "</Tr>";
           strOldModifiedData += "<Tr>";
           strOldModifiedData += "<Td>";
           strOldModifiedData += "Non Modified Item ID";
           strOldModifiedData += "</Td>";
           strOldModifiedData += "<Td>";
           strOldModifiedData += properties.ListItem.ID.ToString();
           strOldModifiedData += "</Td>";
           strOldModifiedData += "</Tr>";
           strOldModifiedData += "<Tr>";
           strOldModifiedData += "<Td Colspan='2' bgcolor='Red'>";
           strOldModifiedData += "</Td>";
           strOldModifiedData += "</Tr>";
           strOldModifiedData += "</Table>";
           string strBindNewData = strNewModifiedData;
           string strBindOldData = strOldModifiedData;
       }

No comments:

Post a Comment