Saving Site as a Template Results in an Error in SharePoint 2010

 When you try to save a template in SharePoint 2010, you may receive an error at Microsoft.SharePoint.SPSolutionExporter.ExportWebToGallery where the system fails to upload the created solution in solution gallery.

 If you receive the Runtime error, first disable custom errors in application web.config. More info here..

To find the cause of problem, try running the command from Powershell by which you can trace ULS log.
$Web=Get-SPWeb http://site
$Web.SaveAsTemplate("Name","Title","Description",1)

The most common error is the duplicate feature folder in farm's 14 hive. If you have saved a site with same name anywhere in the site, try retracting the solution first. As soon as the feature folder is available again, the template will be saved/copied successfully.



If you have a great SharePoint idea, we'll develop it for free!


How to override Core Results Web Part's limit of 50 results per page?

By default "Core Results Web Part" can result a maximum of 50 results. But grouping results require as many items as per requirement. To achieve this, we need to create our own CoreResults Web Part. The following code snippet help to achieve that.


public class cvsearch : CoreResultsWebPart
    {
        protected override XPathNavigator GetXPathNavigator(string viewPath)
        {
            QueryManager queryManager = SharedQueryManager.GetInstance(Page).QueryManager;
            queryManager[0].ItemsPerPage = 100;
            XmlDocument xmlDocument = queryManager.GetResults(queryManager[0]);
            return xmlDocument.CreateNavigator();
        }
    }

How to get the folder of SPListItem?

Need to get the item's parent folder in an event/feature receiver? The code snippet below seems to be the most practical way to do that.

foreach(SPListItem item in Items)
{
    SPFile fItem= item.Web.GetFile(item.Url);
    SPFolder folder = fItem.ParentFolder;
}

How to get recurrence events? For Today and for this Month?

SharePoint provides powerful calendar object model support for developers to leverage on. As it suffers from the lack of kickass documentation, one gets used to it with practice.

Recurring events are such. The major task with such events is to query them correctly so as to avoid in code checks which can burden the web part's performance.So use SpQuery properties correctly.

For example, to get all the recurrence events for this month:

//Get the first day of the Month
                DateTime caldate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                SPQuery query = new SPQuery();
                //Check the  tag, that's important to retrive a month of events including the recurrence one
                query.Query = "";
                query.ExpandRecurrence = true;
                //feed the month's first day to query
                query.CalendarDate = caldate;
                //fire the query to get list of items
                SPListItemCollection items = lst.GetItems(qry);

As a second example, to get all the recurrence events for today
//Get the date for today
                DateTime caldate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
                SPQuery query = new SPQuery();
                //Check the  tag, that's important to retrive all events including the recurrence one for today
                query.Query = "";
                query.ExpandRecurrence = true;
                //feed the month's first day to query
                query.CalendarDate = caldate;
                //fire the query to get list of items
                SPListItemCollection items = lst.GetItems(qry);

So if the dates are correct and CAML are well formed, we get our results correctly.