Friday 27 September 2013

PowerShell script to delete all items in a List



Deleting items from your list can be done in a easier way using the shell script. 
This is much easier when compared to deleting items from the browser. 

1) Copy & paste the following script in a notepad. 

2) Provide your site name mentioned at $SITEURL = "http://your server"

3) Provide your list name as it in browser mentioned at $oList = $web.Lists["Your List"];

4) Run Sharepoint 2010 Management Shell as administrator.

5) Copy paste the script from notepad and press enter.

6) Now all the items at particular list will be deleted.


$SITEURL = "http://your server"

$site = new-object Microsoft.SharePoint.SPSite ( $SITEURL )
$web = $site.OpenWeb()
"Web is : " + $web.Title

$oList = $web.Lists["Your List"];

"List is :" + $oList.Title + " with item count " + $oList.ItemCount

$collListItems = $oList.Items;
$count = $collListItems.Count - 1

for($intIndex = $count; $intIndex -gt -1; $intIndex--)
{
        "Deleting : " + $intIndex
        $collListItems.Delete($intIndex);
}


Tuesday 24 September 2013

SharePoint 2010 - "Unable to display this Web Part"


                                  I was using web parts in a page and i had faced this problem. It was telling that "Unable to Display this web part. To trouble shoot the problem, open this Web Page in Microsoft Sharepoint Foundation - compatible HTML editor such as Microsoft Sharepoint Designer. If the problem persists contact your web server administrator. Corelation ID : 7acacbd8...  "



                                 When i look in the error log using the Correlation ID, I get this.


Error while executing web part: System.StackOverflowException: Operation caused a stack overflow.     at Microsoft.Xslt.NativeMethod.CheckForSufficientStack()     at <xsl:template match="FieldRef[@Name='Author']" name="FieldRef_header.Author" mode="header">(XmlQueryRuntime , XPathNavigator , Double )     at <xsl:template match="View" mode="full">(XmlQueryRuntime , XPathNavigator , String )     at <xsl:template match="View" name="View_Default_RootTemplate" mode="RootTemplate">(XmlQueryRuntime , XPathNavigator , String )     at <xsl:template match="/">(XmlQueryRuntime )     at Root(XmlQueryRuntime )     at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer, Boolean closeWriter)     at System.Xml.Xsl.XmlILComman...        7acacbd8-e819-4018-ae84-1900dd9c10e4


                               It tells me that stack over flow exception has occurred. Now when i refresh the page for the second time the web part works fine for me. The problem is only when the page is initially loaded.

                              The problem is a XSLT extension method taking long time to complete. in SP2010 SP1 the SharePoint team introduced a check in the XSLT Transformations done by data from web part and thereby in search core results webpart, that the latest extension method has to complete within 1 second of the first being invoked otherwise they throw StackOverflow Exception. The solution is changing the duration of the webparts in all forms available from 1 second to 5 seconds. To do so follow the method.

1) Locate Sharepoint 2010 Management Shell 

2) Paste the following script and enter

$farm = Get-SPFarm 
$farm.XsltTransformTimeOut = 5 
$farm.Update()

This will change the timeout from 1 second to 5 seconds which gets us some time so that StackOverflow Exception wont occur.






Sunday 15 September 2013

Check if list exists using TryGetList method in SharePoint 2010


Now lets see how to check if list exists using TryGetList method in SharePoint.

Normally we will be using 

SPList list = web.Lists[listName]; 

to get a list.

But, it throws a null reference error if the list is not there(i.e., in case if it is being deleted from the site using browser). So we will be looping through the SPListItemCollection object and check if the list exists or would go for exception. In SharePoint 2010, a new method "TryGetList" is implemented to get the list and check if the list exists. Lets see how the code works out.


using (SPSite site = new SPSite("http://serverName:1234/"))
{
  using (SPWeb web = site.RootWeb)
     {
        SPList list = web.Lists.TryGetList("My List");
        if (list != null)
        {
          Console.WriteLine("List exists in the site");
        }
        else        

        {
          Console.WriteLine("List does not exist in the site");
        }
          Console.ReadLine();
     }
}