Thursday, 14 July 2011

Count the Number of Approved,Rejected and Pending Status in Document Library through CAML Query and Maping it to another List using Event Receiver

 public override void ItemUpdated(SPItemEventProperties properties)
       {
           base.ItemUpdated(properties);
           try
           {
            
        if (properties.List.Title == "SampleLibrary")
               {
                   SPSite oSite = new SPSite("http://sharepoint:4747/");
                   SPWeb oWeb = oSite.OpenWeb();
                   SPList oList = oWeb.Lists["SampleLibrary"];
                   SPQuery spQuery = new SPQuery();
                   spQuery.Query = "<Where><Eq><FieldRef Name='Status' /><Value Type='Text'>Approved</Value></Eq></Where>";
                   SPListItemCollection items = oList.GetItems(spQuery);
                   string s = items.Count.ToString();
                   SPQuery spQuery1 = new SPQuery();
                   spQuery1.Query = "<Where><Eq><FieldRef Name='Status' /><Value Type='Text'>Approval Rejected</Value></Eq></Where>";
                   SPListItemCollection items1 = oList.GetItems(spQuery1);
                   string s1 = items1.Count.ToString();
                   SPQuery spQuery2 = new SPQuery();
                   spQuery2.Query = "<Where><Eq><FieldRef Name='Status' /><Value Type='Text'>Draft In Progress</Value></Eq></Where>";
                   SPListItemCollection items2 = oList.GetItems(spQuery2);
                   string s2 = items2.Count.ToString();
                   SPList oList1 = oWeb.Lists["SampleList"];
                   SPListItem itemToUpdate = oList1.GetItemById(5);
                   itemToUpdate["Status"] = "Approved";
                   itemToUpdate["Count"] = s;
                   itemToUpdate.Update();
                   SPListItem itemToUpdate1 = oList1.GetItemById(6);
                   itemToUpdate1["Status"] = "Approval Rejected";
                   itemToUpdate1["Count"] = s1;
                   itemToUpdate1.Update();
                   SPListItem itemToUpdate2 = oList1.GetItemById(7);
                   itemToUpdate2["Status"] = "Draft In Progress";
                   itemToUpdate2["Count"] = s2;
                   itemToUpdate2.Update();


               }
           }
           catch (Exception exe)
           {
               throw (exe);
           }
          
       }



    }
}

No comments:

Post a Comment