Wednesday, April 27, 2016

Archiving Absence of Leave calendar in SharePoint 2013

In our Company we have Absence of Leave calendar, where all employees add their vacations, sick days, holidays etc. Over the years this calendar grow quite a bit, with over 10.000 entries. As you are aware, Microsoft recommendation for entry numbers is no more than 5.000. Recently we have experienced some strange behavior in this list, some entries were duplicated, views did not work as it should. All in all, it was time to archive this list and start with fresh one. 

First thing is to create new calendar. Because Absence calendar has various columns and views, the easiest way was to save current Absence calendar as template, and then to create Absence-new calendar from that template. So, first go to Absence calendar, open Calendar tab and choose List Settings:

Under Permissions and Management click Save list as template option:

Choose a File Name, Template Name and Template description, in my case I called it Absence-template. Leave Include Content box unchecked, we want to save empty list, and then click OK

Once we have created template, we can use it to create new calendar. Go to Site Contents, choose Add an App, in search field type absence, in order to find Absence-template, choose Absence-template and in Name field type Absence-new. Upon clicking on Create button new Calendar will be created.

Now we have old calendar named Absence and new calendar called Absence-new. Next step is to rename them, so Absence will become Absence-archive, and Absence-new will become Absence. Go to Absence calendar, open Calendar, choose List Settings, and under General Settings click List name, description and navigation:

Change calendar name to Absence-archive and in Navigation section remove this list from Quick launch:

Do the same steps to Absence-new calendar, change its name to Absence and display this calendar to Quick launch.

This is as far as we can go with GUI, from now on we are going to need PowerShell.

Let us summarize what we did so far; we have created template from old calendar allowing us to use this template in order to create new calendar with same columns and views. After that we renamed old calendar to Absence-archive and remove it from Quick launch, also we renamed new calendar to Absence and added it to Quick launch. You may think that this is all we must do, but you would be wrong. We did change calendar names but URLs to list remained the same. URL to list Absence-archive is:

http://ift.tt/1SzZUYO

and for Absence:

http://ift.tt/1VBC0BV

so we must change root folders for these calendars. Why to do this, you may wonder. I can think of three reasons; first one would be that some services and scripts are using data from this calendar, so if don't change root folder they will not collect data from active calendar. Second reason is that some users have bookmarked this calendar, and will keep on looking in old calendar. This could lead to some misunderstandings and communication problems. And third one is that there will always be someone asking why there is difference between calendar names and URLs, and if this question escalate, you will end up changing URLs. So why not do it right now?

Calendar URL can be changed on few different ways, but easiest way is through PowerShell. Open PowerShell management console and run this script:

Add-PSSnapin Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue

$libOriginalUrl = "/Lists/Absence";
$libNewUrl = "/Lists/Absence-archive";
$web = Get-SPWeb -Identity http://ift.tt/1VBC0BX

$lib = $web.GetList($web.Url + $libOriginalUrl)
$rootFolder = $lib.RootFolder;
$rootFolder.MoveTo($web.Url + $libNewUrl)

Run this script both for Absence and Absence-archive calendar, by changing $libOriginalUrl and $libNewUrl.

Once we changed URLs all is left to do is to add data to new Absence calendar. Because this is Absence calendar, there are events which are recurring, like religious holidays, or domestic holidays. Also there is need to add events which are still in progress, or better yet, which end date is still in the future. We can not ask employees to insert events again just because we archived calendar. 

To add active events to new calendar we need PowerShell script. This script get all events which end date is greater that now, and copy event fields to new calendar:

Add-PSSnapin Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue

$web = Get-SPWeb -Identity http://ift.tt/1VBC0BX
$sourcelibrary = $web.Lists["Absence-archive"]
$destlibrary = $web.Lists["Absence"]
$sourcefields = $sourcelibrary.Fields

$sourceitems = $sourcelibrary.GetItems() | Where-Object {$_["EndDate"] -gt (Get-Date)}
foreach ($item in $sourceitems) {
    $newdestitem = $destlibrary.AddItem()
    foreach ($field in $sourcefields) {
        if (($field.ReadOnlyField -ne $True) -and ($field.InternalName -ne "Attachments")) {
            $newdestitem[$($field.InternalName)] = $item[$($field.InternalName)]
            }       
        }
        $newdestitem.Update()
    }

Upon successfully updating of events, your new calendar is up and running. With PowerShell scripts whole process of archiving calendar can last just few minutes, depending of number of events we need to copy.


by Krsto Savic via Everyone's Blog Posts - SharePoint Community

No comments:

Post a Comment