Monday, April 21, 2014

The Feature is not a Farm Level Feature and is not found in a Site level defined by the Url


we can get all features ids using following command  


get-SPFeature

Active the feature using following command  


Enable-SPFeature -Identity FeatureId -URL SiteCollectionURL -Force -PassThru
(ex:
FeatureId : db39db05-c961-402f-b323-4dc6f1c68ff4
SiteCollectionURL :  http://htshydasm:1111/sites/Development/ )

Tuesday, April 15, 2014

Delete all Items from sharepoint list

 Hi this is the sample code for delete the list items at a time using batch
 this increase the performance of the delete Query 
 using following code i deleted list items whose created date is less than the 3 years ago


 StringBuilder sb = new StringBuilder();
            using (SPSite osite = new SPSite("http://htshydasm38/sites/TestSite/"))
            {

                using (SPWeb oweb = osite.OpenWeb())
                {
                    try
                    {
                       // SPSecurity.RunWithElevatedPrivileges(delegate()
                       // {                       
                            SPList taskList = oweb.Lists["TimerList"];                       
                            SPListItemCollection itemCollection = taskList.Items;
                            int count = 0;
                            StringBuilder sbDelete = new StringBuilder();
                            sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
                            string command = "<Method><SetList Scope=\"Request\">" + taskList.ID
                                                         + "</SetList>&ltSetVar Name=\"ID\">{0}</SetVar>
                                                          <SetVar Name=\"Cmd   \">Delete</SetVar></Method>";
                            foreach (SPListItem it in itemCollection)
                            {
                                string CeatedDate = Convert.ToString(it["CreatedDate"]);
                                if (!string.IsNullOrEmpty(CeatedDate))
                                {
                                    DateTime CreatedDate = Convert.ToDateTime(it["CreatedDate"]);
                                    DateTime CurrentDate = DateTime.Now;
                                    DateTime ThreeYearsoldDate = CurrentDate.AddYears(-3);
                                    sb.Append("Created Date:" + CreatedDate + " ");
                                    sb.Append("CurrentDate:" + CurrentDate + " ");
                                    sb.Append("ThreeYearsold Date:" + ThreeYearsoldDate);
                                    sb.Append("");
                                    if (CreatedDate < ThreeYearsoldDate)
                                    {
                                        count += 1;
                                        sbDelete.Append(string.Format(command, it.ID.ToString()));
                                    }
                                    Label1.Text = "Count" + count + " ---" + sb.ToString();
                                 
                                    Label2.Text = sbDelete.ToString();
                                }
                            }
                            sbDelete.Append("</Batch>");
                            oweb.AllowUnsafeUpdates = true;
                            if (count > 0)                           
                                oweb.ProcessBatchData(sbDelete.ToString());                           
                           oweb.AllowUnsafeUpdates = false;
                       // });                      
                    }
                    catch (Exception ex)
                    {                     
                        Label1.Text = ex.Message.ToString();
                    }
                }
            }