Thursday, 19 March 2015

How to hide SharePoint list group by labels

Hello Reader! Hope you are fine.
In this post I am going to tell you a very nice and easy trick to hide the group by labels or headings in grouped SharePoint list view. I don't know if you have faced such requirement yet but trust me there is a huge possibility of it. It is weird though isn't it? Why would you even want to hide it? If you have applied a group by why would you want to hide it? I don't know about you guys but the first thing that came to my mind is this. But anyways we have to do what our 'kindhearted' and 'soft-spoken' boss wants us to do. So lets start our mission!

There are 2 ways of doing it . One is to use CSS and other is using JavaScript. I'll be using JavaScript here because that's more flexible and you know that's what I do... provide you better solutions ;-).
Okay lets get serious now. We will use JavaScript in content editor web part to achieve our target.

First step is to note down the class name of the group by labels you want to hide. You can find it out using developer tools in our browsers.
How to find a page element
  • Simply press F12 and you can see a small pane with page source.
  • Then select the element (group by label) to find it in page source. If you don't know how to select an element don't worry. For Internet Explorer click on the button with arrow symbol on top left corner. Or in Google Chrome click on the button that looks like a magnifying glass, then click on the element on page that you want to find on the page source.
  • Now find the class name of the group by labels. It may look something like "ms-gb". It will be same for all the labels through out the page.
Now that we have the class name of the elements we want to hide, we can proceed further.
Add a Content Editor Web Part to your page(where you want to hide labels).

How to add Content Editor Web Part
 
Steps to add a Content Editor web part :
  • Click on the site settings button.
  • Click on 'Edit Page'.
  • Click on 'Insert' in Ribbon on top.
  • Click on Web Part.
  • Select 'Media and Content' from category of web parts.
  • Then select Content Editor and click Add button.

Next click on the Content Editor web part and then click on 'Edit Source' on top.


Then you can write your code in a box that pops up. You can write this code to hide the group by labels :

<script language="javascript">
window.onload = function ()
 {
var myele = document.getElementsByClassName('ms-gb')
for(var i=0;i<myele.length;i++)
{
myele[i].style.display="none"
}
// if you have more that one group by condition 
var myele1 = document.getElementsByClassName('ms-gb2') 
for(var i=0;i<myele1.length;i++)
{  
myele1[i].style.display="none"
}
}
</script>

I must warn you this may cause flickering on screen. By flicker I mean the group by labels would be first visible for a second or so then it will disappear. This may not look so good. But only way that I know of to avoid this is to use CSS in page source. But editing the page source cannot be an option always. Like in my case it wasn't an option (so I had to live with it) because 1--> It was client's site   2--> If you hide it using CSS in page source it will hide the labels in all the pages of the list view. I'll explain this.

In your SharePoint list if you have folders or document sets on first level and then data inside it, CSS in page source or the JavaScript I have written above will hide the labels in all the levels(i.e for the items inside folders also). If you want to hide it only in say, first level you have to tweak the code. You have to differentiate the first page of the list from other pages (pages that come after clicking on folders) in your code. Now you understand why I had said earlier JavaScript is more flexible approach?
If you look closely when you click on a folder in your list, it doesn't go to a new page, it just adds parameters to it. For example if your list URL is something like this - https://servername/sites/sitename/mylist.aspx , then clicking on folder will take you to "siteurl/mylist?RootFolder=%something%". So I thought of a simple way to apply the script only on first page.
Use the JavaScript 'slice' function to get the last part of the URL.

<script language="javascript">

window.onload = function ()
 {

var url = document.URL          //considering your list url is the one given above
var lastpart = url.slice(-11,-1)  //this returns mylist.asp (without the 'x')
if(lastpart == "mylist.asp" || url.indexOf('mylist.aspx?View')>0)
 {
  var myele = document.getElementsByClassName('ms-gb')
  for(var i=0;i<myele.length;i++)
  {
   myele[i].style.display="none"
  }
  var myele1 = document.getElementsByClassName('ms-gb2')
  for(var i=0;i<myele1.length;i++)
  { 
   myele1[i].style.display="none"
  }
 }

</script>

We have used "url.indexOf(''mylist.aspx?View)>0" because when you go back to first page of list from inside of any folder by clicking on bread crumb link of list on top it appends '?View' to the URL.

Finally I would like to mention that if you have multiple lists where you want to implement this you don't have to place this code in content editor in each of the pages. You can write your code something like :

<script language="javascript">

window.onload = function ()
 {
var url = document.URL
if(url.indexOf('mylist.aspx') > -1) // to identify if
{

 var lastpart = url.slice(-11,-1)
 if(lastpart == "mylist.asp" || url.indexOf('mylist.aspx?View')>0)
 {
    var myele = document.getElementsByClassName('ms-gb')
  for(var i=0;i<myele.length;i++)
  {
   myele[i].style.display="none"
  }
  var myele1 = document.getElementsByClassName('ms-gb2')
  for(var i=0;i<myele1.length;i++)
  { 
   myele1[i].style.display="none"
  }
 }
 }
else if(url.indexOf(mylist1.aspx') > -1)
{
 var lastpart = url.slice(-12,-1)
 if(lastpart == "mylist1.asp" || url.indexOf(mylist1.aspx?View')>0)
 {
  var myele = document.getElementsByClassName('ms-gb')
  for(var i=0;i<myele.length;i++)
  {
   myele[i].style.display="none"
  }
  var myele1 = document.getElementsByClassName('ms-gb2')
  for(var i=0;i<myele1.length;i++)
  { 
   myele1[i].style.display="none"
  }
 }
 }

</script>

Then you can paste this code in a text file(.txt) and upload it in style library (in Site Contents) of your top level site (not in subsite) . Then paste the link of this txt file in content link of content editor.
  • Click on Edit page
  • Click of content editor Edit Web Part
  • paste the link of the text file with script in content link.
  • Click OK.
After this the Content Editor web part gets the code from the file in style library. Its a better way to manage your codes.

SharePoint Facts

SharePoint 2013 came with a lot of new features. One of them is "Device Channels". It was introduced to enable users to view SharePoint sites in mobile devices like smart phones, tablets etc.
You can create different device channels for different devices and specify different master pages or CSS files for different devices. It matches the 'User Agent' substring that comes with a Http request from device browser with the device channels User Agent substring that we provide in inclusion rule like "windows phone OS" etc and applies the CSS file for that device. Pretty cool right?

Friday, 13 March 2015

SharePoint : Update() vs SystemUpdate() vs UpdateVersionOverwrite()

As Arnold in 'The Terminator' I am back as a savior but ofcourse with SharePoint solutions. Today I will tell you the difference between 3 little methods to update a SharePoint list item that often confuse people new to SharePoint. It confuses because all the three functions have same basic functionality i.e. update an item. Then how to decide which one to use? Below are the differences :

                 Update                                SystemUpdate                              UpdateOverwriteVersion        

Updates the item in database


Updates the item in database

Updates the item in database

Updates the 'modified' and 'modified by' value

Doesn't change the 'modified' and 'modified by' fields. Updates the 'modified' and 'modified by' value

Creates a new Version

Doesn't create a new version Doesn't create a new version

NOTE : While working on one of such requirements (in SharePoint 2013) I have found out that UpdateOverwriteVersion doesn't increment the minor versions but if the version of an item is a major it increments its version. For example if your item version is 2.0 it may change it to 2.1. If you are not okay with that you can use SystemUpdate. If you want to preserve the metadata i.e. if you want the "modified" and "modified by" fields to remain unchanged you can add these to lines to your code :

$item["modified"] = $item["modified"]
$item["modified by"] = $item["modified by"]

(For SystemUpdate you don't have worry about it. But if these 2 fields are getting updated you can preserve it like this.)
what it does is keeps your field values unchanged. If it doesn't work you can try it with $web.AllowUnsafeUpdate=$true. After you are done with all the changes you can set it to false. 'AllowUnsafeUpdate=$false' protects you from cross site scripting.

Okay... this is all I had to say on this topic. I have decided to feed you with some interesting facts about SharePoint at the end of every article from now on.

FACT : In SharePoint a list or library item can only occupy a maximum of 8000 bytes in content database out of which 256 bytes are reserved for built-in columns.

Wednesday, 4 March 2015

Export/Import SharePoint List/Library using PowerShell

Hi all! I am back again as promised with my next blog. In my last blog (Migrate SharePoint List\Library from one site to another site in different server) I discussed how we can take a backup or export a SharePoint list or library from source site and restore or import it in target site using 'save as template' approach and the 'Granular Backup' approach.

Now we will explore the most flexible way of doing it and undoubtedly the most powerful way...The PowerShell! PowerShell is a tool based on .Net framework. Commands that I will be discussing here can also be used in SharePoint Management Shell. I will be showing how to use these commands in PowerShell. Here are links to different sections of this article.



How to use Export Command

First we need to export the list/library from source site in any server,right? For that we have a very handy cmdlet i.e Export-SPWeb. This is also possible through stsadm command line but since that is deprecated since SharePoint 2010 its use is not encouraged. Actually the export of library is a new feature that is added in SharePoint 2013. So lets see how this command works.

Add-PSSnapin Microsoft.SharePoint.PowerShell
cls
Export-SPWeb -Identity "URL of source site" -path "Path\FileName.cmp"
-ItemUrl "internal name of list\library" -IncludeUserSecurity                                                          -IncludeVersions "CurrentVersion" -NoFileCompression -Verbose
Write-Host "List Backup Done!"

This is a very simple script to export a list.
For those of you who are completely new to the PowerShell, don't panic I am gonna walk you through all the statements and parameters.
First statement 'Add-PSSnapin' is used to add one or more snap ins to current PowerShell session. Here it registers a set of cmdlets specific to SharePoint. You can think of it as '#include' in C. :-)
'cls' is just used to clear output screen to make it look neat and less confusing. Next comes the main command- Export-SPWeb. You can use this command to export a site collection,list or library.
Its various parameters and their functions are as follows :
  • Identity - Here we have to pass the url of the site which contains the list we have to export.
  • Path - This specifies the path (including the file name with .cmp extension) where the backup of the list or site is stored.
  • ItemUrl - Here we have to pass the url of the list or library. For example for a list it may be like "/lists/mylist". Note: For libraries it will be like "/LibraryName" without "/lists/". You can get this from the url of the list in the browser.
  • IcludeUserSecurity - This parameter, if present will preserve the security settings of the list that we are exporting (only if the list doesn't have a broken inheritance or item level permission).
  • IncludeVersions - This indicates the list item version history to include in the export operation. It can take values of - LastMajor | CurrentVersion | LastMajorAndMinor | All. If we don't use this parameter it takes default value i.e LastMajor.
  • NoFileCompression - This parameter is used to enable or disable file compression. If you have large number of items you can use file compression to increase your performance. It is estimated to increase the performance by 30%.
There are many more parameters. If you want a detailed explanation you can visit this site.
Note: If you want to overwrite existing export files in the given path you can use -Force parameter.

Sounds simple right! Just write the command and required parameters and you have your list backup ready with versions also. Now our aim is to import it to any target site.

How to use Import Command

Add-PSSnapin Microsoft.SharePoint.PowerShell
cls
Import-SPWeb -Identity "Your target site url" -UpdateVersions Overwrite                                  -IncludeUserSecurity        -Path "yourpath\file.cmp" -NoFileCompression -Verbose
Write-host "Import Completed!"


This command here imports the list that you had exported using the Export-SPWeb command on top.
Below is the explanation for parameters I have used :
  • Identity -  We have to pass the url of the target site where we want to restore the list.
  • UpdateVersion - This parameter is used to specify what do you want to do in case the file being imported is already present in the destination library. It takes three values Add, Overwrite, Ignore. Add just adds the file with conflict as a new file with different version. Overwrite as the name suggests replaces the old files (for only the files with conflicts) with new files that were exported earlier. What it does is while importing if a file is found to be already present in the target lis then it deletes all the old versions of the file and then imports it. Ignore doesn't import the file in case of conflict. It just leaves the file as it is.
  • Other parameters work exactly the same way as in case of Export-SPWeb.
NOTE: You can use -NoFileCompression in Import command only if you have used it while exporting.
NOTE: If the library from which you have exported is already present in destination site it imports the items in that library else it creates a new library by that name.

There is an interesting scenario I will discuss here from my own experience here. These commands work perfectly even if the version setting in SharePoint library is disabled too. We had this requirement in our project and we were not sure how does these commands behave in case the version setting is disabled. So after some research I found out that you can export current version for all the items and then import as overwrite. In case of SharePoint library it compares the 'Name' field of library (which has the Name of document), to determine if the item is already present. Then it just overwrites all the other changes in item. If the 'Name' field of item is changed then it creates a new item in destination list.

So now we know how to shut the mouth of our pushing boss who wants us migrate all the SharePoint content from one server to another. I have tried my best to make it understandable and simple. I am not a professional blogger. So if there is any improvement you want please do leave comments or suggestions so that it will help us both. If there is any query feel free to ask in comments.

I'll be back! Hasta La Vista.

Sunday, 1 March 2015

Export/Import SharePoint List/Library from one site to another in different server

When you are working on a SharePoint Project you may face the requirement to migrate a list or library from one site to another site (could be on same or different server) more often than you think.
If your boss is pushing you to migrate a list from one site to another and you have no clue about how to do it, you have reached the right place.
There are 3 ways that I know of in which this can be achieved.
I am going to explain all three options with special focus to the third one because that's the most important and the most flexible.

Save List/Library As Template

While the first one is the easiest of all, it provides the least flexibility. All you have to do is :
go to the list/library settings and click on save list as template. You can choose to save the template with or without content. You should have permission to manage lists.

There are two drawbacks with this method.
  1. If the list or library is very large in size then it becomes very difficult to take backup and restore. There is a workaround for this also. But that requires you to be SharePoint administrator. What you can do is, increase the maximum upload size limit in central admin. In SharePoint 2013 its 200 Mb by default. Go to central admin --> Manage Web Application --> Select Application -> General Settings  --> set the maximum upload size.  For more information you can visit this link.
  2. Second issue is if you already have a list in your target site and you want to overwrite it or just add new items that is not possible using this method.
  3. Security settings of the list are not saved in the .stp file (list template).
If these three drawbacks are not your concern then you can go ahead with this option. Just download the saved list from source SharePoint site(.stp file) and upload it in your target site (in any server, just copy and paste the .stp file in target server) just like normal files. You can upload it in solutions gallery in site settings page.
List templates include all the columns, fields and views that you have created for the list.
For more information click here.

Granular Backup and Restore

Second approach is to use the "Granular Backup" option available in central administration page under Backup and Restore. All you have to do is :

  1. Select the site collection and site.
  2. Select the list that you want to backup.
  3. Give the path where you want to store the backup including the filename.cmp. eg- C:\backup\listname.cmp.
  4. You can check the export full security check box. It will export the users in the site.
  5. You can select what versions of files in the list you want to export (All versions, Current version etc)



As you can see this option has comparatively more flexibility than first option. But there is no facility in central administration to help import a list/library through UI. You have to use PowerShell command to import the list to target site.

Now we are left with the last option. Since that is big topic and needs special attention I am going to cover that in my next blog stay tuned. Stay healthy! Oh and please don't forget your valuable comments and suggestions so that I can provide better quality blogs. Peace Out!