Saturday 18 April 2015

SharePoint List View Threshold

Hello Readers!
In this post I am gonna tell you about SharePoint list view threshold.

List View Threshold

List view threshold is a feature in SharePoint that allows you to limit maximum number of items that can be returned in a query for a list or library. When you understand list view threshold properly you will realize that its your friend not your foe. List view threshold is in place to improve the performance of SharePoint. The default value of list view threshold is 5000 items. Now you may ask why 5000?? What's so special about it? To answer this question we need to go to the back end. SharePoint stores all the data of a site collection in one table called AllUserData. And SQL Server locks a table if the query result exceeds 5000. So if you click on a list view which has more than 5000 items SQL Server will lock the AllUserData table till your query is finished. Till that time all the other users that may be trying to access data in other lists or libraries will have to wait. It's not just this. If you increase the list view threshold then the number of requests that SharePoint can handle decreases drastically.

Still sometimes it becomes necessary to increase the list view threshold limit till you find some permanent solution. You can do that by going to Central Admin --> Manage Web Application --> select your web application --> General Settings --> Resource Throttling.
But remember this is not permanent solution. There are few ways in which you can solve the puzzle of list view threshold. One is that you can create views and apply filters to limit the number of items returned for that view. But we know its not possible every time. So other solution is to move the items into folders on some basis of some categories. For example you can move documents into the folders of years and months according to their Created By date. In my next blog post I will explain how you can move large number of list items into folders using PowerShell Script.

Stay Tuned!

Sunday 12 April 2015

PowerShell : Regular Expressions

Greetings people of Earth!

In this article we will discuss about Regular Expressions in PowerShell. Those who have watched Interstellar movie already know that in some situations time becomes precious for us like any other resources. We have to save it! And those who have been in a situation where our dear and kind boss shouts at us "why is this not done?", "why is your program taking so long to run?", "What kind of shit code have you written", know even better the value of time. And that's why I am writing about Regular Expressions.

The beauty of regular expression is that its faster than other string searching or matching methods. But it might seem complicated if you don't understand them. But once you understand them, they become your best friend. Now you may ask why is it faster?
Regular Expressions are faster because they use smarter algorithms like Boyer-Moore string search algorithms. It doesn't go through the whole text matching all the characters with the pattern. It skips characters in between which improves the performance. If you want to learn more on that you can visit the link above.

Before diving into regex functions let's take a look into some basic symbols you will come across.

         
          SYMBOL                  
                                       MEANING                                           
               \ used to instruct compiler not to consider any special meaning of the next character.
               * represents 0 or more characters. Ex- a*
               + represents 1 or more characters. Ex- a+
               ? represents 0 or 1 character. Ex- a?
               . match any character except newline.
               [] match any single character from within the brackets. you can also specify range like [a-z] or [0-9]
               [^] match any character except those in brackets.
               ^ represents the beginning of a line. Ex- ^ab matches 'ab' in the beginning of newline.
               $ represents the end of line.
               {n,} used to represent a pattern repeated n or more number of times.
               {,n} used to represent a pattern repeated n or less than n number of times.
               {n} used to represent a pattern repeated exactly n of times.

These are some symbols you will find more often while working with regular expressions. Now I will show you some examples of regular expressions. I would suggest you to first think yourself to form a regex before looking into pattern I have given.

    - apple is a fruit apple
suppose you want to remove the second 'apple' then your regular expression would be "apple$"

$string = "apple is a fruit apple"
$string = $string -replace "apple$" , ""

    - CN=Andy/OU=ROY/AP=US
Suppose this is a string from which you want to remove the character before '=' so that resulting string is Andy/ROY/US. 

$string = "CN=Andy/OU=ROY/AP=US"
$string = $string -replace "[A-Z]{2}=",""

write-host $string

This expression replaces 2 alphabets before "=" with a blank.
NOTE : "replace" function is case insensitive so it considers it will consider "cn=" and "CN=" the same.
Suppose our string is : "Cn=Andy/OU=ROY/AP=US"
and you don't want to remove small case letters then you can use below script

$string = "Cn=Andy/OU=ROY/AP=US"
$string = $string -creplace "[A-Z]{2}=",""
write-host $string

This is will give below result :



Suppose the string is :
"C=Andy/OU=ROY/APS=US"
Then you can use the below script :


Here '[A-Z]{1,3}=' matches atleast 1 and atmost 3 alphabets before '='.
Now suppose you have a string like this :
"<body><p attr="some attribute value">some text that you want to retain</p></html>"
This is a scenario that I have faced where a SharePoint column had all junk data like this from source and I had to remove all the unwanted characters. I have simplified the string and kept only html tags to make it simple for you.
You can remove all the html tags with this regex : "<.+?>"



This is the power of regular expressions. The longer your pattern is the faster it works. There are still many types of patterns to discuss. But it's not possible to discuss all scenarios. So if you have any doubts regarding any regular expression feel free to comment below or mail me.
Have a nice day!

Monday 6 April 2015

SharePoint : Update Managed Metadata Column Using PowerShell

Greetings Readers!
In this blog we will discuss how to update the managed metadata column of a SharePoint site. Those who have worked on it can understand how frustrating it is to try to update it without knowing the proper way. I remember the first time when I had tried to write a PowerShell script to update values of a taxonomy field of document library. I was stuck like anything. But I assure you once you know the correct way to reference a SharePoint Managed Metadata field it becomes very easy.

Here is a sample script showing how to do it.

try{
$siteobj = new-object Microsoft.SharePoint.SPSite("http://your-site-url")
$web = Get-SPWeb "http://your-site-url"
#creating the taxonomy session object
$session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($siteobj)
if($session -eq $null)
{
        Write-Host "Failed to create taxonomy session"
}
#Getting the term (new value) to which we want to update the column
$termstore = $session.TermStores["termstore name"]
$group = $termstore.Groups["group name"]
$termset = $group.TermSets["termset name"]
$term = $termset.Terms["term name"]
#List/Library we want to update
$list = $web.Lists["your list name"]
$taxfield =  $list.Fields["your column"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField]
foreach($item in $list.Items)
{
      #SetFieldValue function is used to update taxonomy field
      $taxField.SetFieldValue($item,$term)
      $item.SystemUpdate()
}
}
catch
{
      write-host $_.exception
}

Finally
{
        if($web -ne $null)
        {
              write-host -ForegroundColor Green "Disposing the web Object.."
              $web.Dispose()
        }  

}

You can make an input xml file from where you can read the values and do it for multiple site collections in one run. Now you can see how simple it is to update the taxonomy fields. In the end we have used SystemUpdate() function to preserve the metadata of the item like modified, modified by etc. If you don't want then you can use Update or UpdateVersionOverwrite. If you are not sure which one to use visit my previous blog where I have explained their differences. If you have any queries you can leave comments or mail me. Now it's time for a SharePoint fact.
--------------------------------------------------------------------------------------------------------------------------

FACT : We all know SharePoint web parts. We can add a web part to a page in a web part zone or outside it. So you will ask what's the difference? The difference is that if you add a web part in web part zone, the properties of that web part are stored in content database and users can view and edit it through browser. But if web parts are added outside the web part zone then it is stored in the aspx page and users can't edit it through browser. They can only see it. So add wisely!