Monday, 26 November 2012

Color coding calendar in SharePoint 2010

 

· Meeting

· Holiday

· Travel

· Other

Here is my calendar with some information.

Click on the image to zoom

Because I think that color coding would help to easily identify event categories, I have created the following custom view (note that I have chosen not to display start and end time).

Click on the image to zoom

Here is the procedure to customize your calendar to be color coded.

Step 1: Create a new column [Color]. This will be used to define the color of the cell based on the category.

On the list settings page, click on Create Colum link. Name the new column “Color”, and set the type of information as Calculated (calculation based on other columns).

The formula is the following:

Finally, set The data type returned from this formula is: to Single line of text.

=IF([Category]="","LightCyan",

IF([Category]="Meeting","DodgerBlue",

IF([Category]="Holiday","Tomato",

IF([Category]="Travel","LawnGreen",

IF([Category]="Other","DarkBlue","")))))

The result of this column will be a color name based on a category. Note that category should reflect ones you choose and color names can be changed.

Step 2: Create a new column [EventText]. This will contains the html code that will display the event text on the calendar view (including the color scheme).

As above, create a new calculated column with the following formula:

="<span style='position:relative;display:inline-block;width:100%;'>

<span style='width:100%;display:inline-block;text-align:center;border:1px solid "&[Color]&";position:absolute;color:"&[Color]&";'>"&[Title]&"</span>

<span style='display:inline-block;width: 100%;background-color:"&[Color]&";text-align:center;border:1px solid;z-index:-1;filter:alpha(opacity=20);opacity:0.2;'>"&[Title]&"</span></span>"

Note that we used the previously calculated column [Color] to define the cell color, and we also use column [Title] to display the event title inside the cell (you can also display Start Time and End Time if requested).
This is up to you to define your own style, this is simply html syntax.

Step 3: Define [EventText] column as the display on the calendar view.

You now need to create a new view for this calendar. This new view should be a Standard Calendar format view. Define properties as you wish. The important part is to display our newly created column [EventText] to replace the default Title.

Click on the image to zoom

Let’s now save the view and check the result.

Click on the image to zoom

Not so good isn’t itL. It displays the html code instead of interpreting it. To solve this, we need to insert JavaScript code into the page.

Here is the JavaScript code (such kind of code can be found in MSDN web site or in some blogs like techtrainingnotes.blogspot.com).

// Color coded calendars for SharePoint 2010
<script>

// TechTrainingNotes.blogspot.com

// load our function to the delayed load list

_spBodyOnLoadFunctionNames.push('colorCalendarEventLinkIntercept');

// hook into the existing SharePoint calendar load function

function colorCalendarEventLinkIntercept()

{

if (SP.UI.ApplicationPages.CalendarNotify.$4a)

{

var OldCalendarNotify =

SP.UI.ApplicationPages.CalendarNotify.$4a;

SP.UI.ApplicationPages.CalendarNotify.$4a = function ()

{

OldCalendarNotify();

colorCalendarEventLinks();

}

}

if (SP.UI.ApplicationPages.CalendarNotify.$4b)

{

var OldCalendarNotify =

SP.UI.ApplicationPages.CalendarNotify.$4b;

SP.UI.ApplicationPages.CalendarNotify.$4b = function ()

{

OldCalendarNotify();

colorCalendarEventLinks();

}

}

// future service pack change may go here!

// if (SP.UI.ApplicationPages.CalendarNotify.???)

}

// hide the hyperlinks

function colorCalendarEventLinks()

{

// find all DIVs

var divs = document.getElementsByTagName("DIV");

for (var i=0;i<divs.length;i++)

{

// find calendar item DIVs

if (divs[i].className.toLowerCase()=="ms-acal-item")

{

divs[i].innerHTML = divs[i].innerHTML.replace(/&lt;/g,'<').replace(/&gt;/g,'>');

}

// find "x more items" links and re-remove links on Expand/Contract

if (divs[i].className.toLowerCase()=="ms-acal-ctrlitem")

{

var links = divs[i].getElementsByTagName("A");

if (links.length==1)

{

links[0].href="javascript:colorCalendarEventLinks();void(0);"

}

}

}

}

</script>

To include the code into the page, there are 2 solutions:On SharePoint Designer, open your SharePoint site. On the left menu, select Lists and Libraries, then click on your Calendar name on the main screen. Right Click on the view you want to color code (or create a new one first) and select Edit File in Advanced Mode.

· Using SharePoint Designer, the JavaScript code will be added to the page code directly

· Using Content Editor Web Part, the JavaScript code is embed in a WebPart.

Using SharePoint Designer

Identify the section below to copy your JavaScript code inside

Save and check the result.

Click on the image to zoom

You can choose to leave items like this or modify CSS styles to remove the green box around your events.

Search the section <style type="text/css"> and replace with this:

<style type="text/css">

.ms-acal-time {

display:none;

}

.ms-acal-selected, .ms-acal-item {

background:none;border:0px;

}

.ms-acal-sdiv span {

margin-left:-47px;

}

.ms-acal-sdiv div.ms-acal-title span {

margin-left:0px;

}

</style>

Save and check the result. Now we have the expected result J.

Using Content Editor Web Part

First of all, the script above should be saved into a txt document and uploaded in one of your site library.

Click on the image to zoom

On your custom Calendar View, select Edit page on the Site Actions menu.

Add a Content Editor Web part to Main section; this web part is located under Media and Content category. Edit the web part. Set ContentLink to your script document’s URL and Chrome type to None (this will remove web part title and borders)

Click on the image to zoom

Save and check the result.

The drawback of this solution is that you will not be able to select other views for this calendar,

where with the SharePoint Designer method, all views are still accessible.

Because of this I prefer to use the SharePoint Designer method.

Thursday, 22 November 2012

Create SharePoint 2010 sites using PowerShell reading an XML file

 

Just a quick post to show how easy it is to create some sites in SharePoint 2010 using PowerShell by reading an XML file. I’m sure there are some powershell experts out there that can improve the code, but if you need to create a revision gateway during lunch this is the place to start.

Lets start by opening PowerShell with modules on the sharepoint server.

Create a sample xml file:

"<Setup>
<Sites>
<TopSiteName>Site1</TopSiteName>
</Sites>
<Sites>
<TopSiteName>Site2</TopSiteName>
<SubSiteName>Subsite2a</SubSiteName>
<SubSiteName>Subsite2b</SubSiteName>
</Sites>
<Sites>
<TopSiteName>Site3</TopSiteName>
<SubSiteName>Subsite3a</SubSiteName>
<SubSiteName>Subsite3b</SubSiteName>
<SubSiteName>Subsite3c</SubSiteName>
</Sites>
</Setup>" | out-file sample.xml

Now we have some data to work with we can create the sites. Three top sites will be created with 2 and 3 subsites below Site2 and Site3. They can be created using:

[xml]$s = get-content sample.xml
foreach ($e in $s.Setup.Sites){
$v = $e.TopSiteName
$b = $e.SubSiteName
new-SPWeb http://sp2010rc/$v -template "STS#0" -addtotopnav -useparenttopnav -name $v
if($b.Length -gt 0) {
foreach ($b in $b){
new-SPWeb http://sp2010rc/$v/$b -template "STS#0" -name $b -AddToQuickLaunch -useparenttopnav
}
}
}

The bit that creates the site is the new-SPWeb command. For information about the command switches run:

get-help new-SPWeb -full

It’s as easy as that! You will now have some SharePoint 2010 sites created in a matter of seconds.


To remove sites you can just use the remove-SPWeb command. This can be scripted in the same way:

[xml]$s = get-content sample.xml
foreach ($e in $s.Setup.Sites){
$v = $e.TopSiteName
$b = $e.SubSiteName
if($b.Length -gt 0) {
foreach ($b in $b){
remove-SPWeb http://sp2010rc/$v/$b
}
}
remove-SPWeb http://sp2010rc/$v

}

Wednesday, 21 November 2012

SharePoint 2010 – How to move a subsite to a different location

  1. Fire up the command line
  2. CD \”Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN”
  3. Export the old URL using the following Command:

stsadm -o export -url http://intranet/website -filename c:\test\backup.cmp

However, when I tried this under my domain and SharePoint administrator account, I got this error:

The Web application at http://intranet/website could not be found. Verify that you have typed the URL
correctly.  If the URL should be serving existing content, the system administrator may need to add a
new request URL mapping to the intended application.

After searching a bit on this, it seemed like the problem may have been a permissions issue.  So, I confirmed that the administrator account I used had SharePoint Farm Administrative rights, as well as site collection administrative rights.  I then tried to run this same command with a designated “SharePoint Administrator” account which also had SharePoint Farm Administrative rights and Site Collection Administrative rights, but then got an Access Denied error.

After spending some time adding and removing rights for both of those accounts to see if I could get this working, I table the issue and moved on to other tasks.  One of those tasks was to delete some old subsites from our SharePoint.  I ended up stumbling upon the Content and Structure link under Site Administration:

And to my surprise, I found that it is possible to select any site and copy or move it:

  1. Select the parent site of the subsite you want to move in the left navigation pane
  2. Check the box next to the subsite that you want to move in the right pane, click the Actions drop-down and clickMove
  3. Then select the destination of the subsite selected in the next dialog box

This method ended up being a lot more straight forward and quicker than the command line option.

I’m glad I found it!

Wednesday, 31 October 2012

PowerShell Commands To List SharePoint Features

The more I work with SharePoint 2010 the more I am starting to love PowerShell. My scripting skills are pretty basic (!) but here are a couple of useful little snippets to get back some information from a site that previously I would have spent a while going through the UI or SharePoint Root – mainly as a reminder for myself!

List all installed features on the farm
It’s really straight forward using the Get-SPFeature command to return all installed features. To provide a little more readability to the output it can be sorted as follows:
By feature display name alphabetically,

1
Get-SPFeature | Sort -Property DisplayName

By feature ID,

1
Get-SPFeature | Sort -Property Id

By feature display name alphabetically and grouped by scope,

1
Get-SPFeature | Sort -Property Scope,DisplayName | FT -GroupBy Scope DisplayName,Id

And to write this to a file to allow for viewing in Notepad, Excel etc,

1
Get-SPFeature | Sort -Property Scope,DisplayName | FT -GroupBy Scope DisplayName,Id > c:\AllInstalledFeatures.txt

List all activated site scoped features
Especially in the case of hidden features it’s sometimes necessary to track down if a feature is active on a site collection. Here’s a quick way of seeing which features are activated for an SPSite:

1
Get-SPFeature -Site http://sitecollectionurl | Sort DisplayName | FT DisplayName,Id

List all activated web scoped features
And only slightly modified from the Get-Help Get-SPFeature -examples text, here is a command to list all web activated featres for a site collection:

1
Get-SPSite http://sitecollectionurl | Get-SPWeb -Limit ALL | %{ Get-SPFeature -Web $_ } | SortDisplayName -Unique | FT DisplayName,Id

Tuesday, 30 October 2012

How to Deploy ItemStyle.xsl at SharePoint site Creation

It is a known issue with SharePoint that ItemStyle.xsl doesn’t get provisioned. Styling for the Content Query Web Parts (CQWP) are done on ItemStyle.xsl file. In most of the SharePoint projects we add ItemStyle.xsl in to Branding feature, but even after the activation of the feature this file won’t get provisioned.

Most of the sites suggest to upload this file manually after creating the site. But ItemStyle.xsl can be uploaded in to the site at feature activation. Few lined of code will do this. Steps are given below.

  1. Add Event Receiver to the Branding feature (Or what ever the feature that Item.xsl File is included)

  2. Override ”FeatureActivated” Event and add below code.

 

 

1 public override void FeatureActivated(SPFeatureReceiverProperties properties)
2 {
3
4 SPSecurity.RunWithElevatedPrivileges(delegate()
5 {
6 bool IsXSLUpdated = false;
7 String fileToUpload = @"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\FirstPrincipal\XslStyleSheet\ItemStyle.xsl";
8 string libraryName = "Style Library";
9 string xslFolderName = "XSL Style Sheets";
10
11 if (IsXSLUpdated == false)
12 {
13 using (SPSite spSite = (SPSite)properties.Feature.Parent)
14 {
15
16 SPFolder xslStylefolder = null;
17 SPWeb web = spSite.OpenWeb();
18 if (System.IO.File.Exists(fileToUpload))
19 {
20 SPFolderCollection stylefolders = web.Folders[libraryName].SubFolders;
21
22 foreach (SPFolder folder in stylefolders)
23 {
24 if (folder.Name == xslFolderName)
25 {
26 //upload replacing the existing
27 xslStylefolder = folder;
28 String fileName = System.IO.Path.GetFileName(fileToUpload);
29 System.IO.FileStream fileStream = System.IO.File.OpenRead(fileToUpload);
30
31 SPFileCollection xslFileCollection = xslStylefolder.Files;
32
33 foreach (SPFile file in xslFileCollection)
34 {
35 if (file.Name == fileName)
36 {
37
38 file.CheckOut();
39 break;
40 }
41 }
42 SPFile spFile = xslStylefolder.Files.Add(fileName, fileStream, true);
43
44 spFile.CheckIn("Checking in item style xsl sheet", SPCheckinType.MajorCheckIn);
45 spFile.Publish("Publishing the item style xsl sheet");
46 xslStylefolder.Update();
47 IsXSLUpdated = true;
48 break;
49 }
50 }
51
52 }
53
54
55 }
56 }
57 });
58 }

Friday, 26 October 2012

Import Data to List using PowerShell by csv

Add-PsSnapin Microsoft.SharePoint.Powershell –ErrorAction SilentlyContinue
$csv = Import-Csv 'E:\Sharepoint\SharepointStuff\PowershellScripts\test\w\Import.csv'

$web = Get-SPWeb -identity "http://globas"

$list = $web.Lists["News"]

foreach ($row in $csv) {
$item = $list.Items.Add();
$item["Title"] =$row.Title
$item["Body"] =$row.Body
$item["Expires"] =$row.Expires
$item.Update()
}


Write-Host "Finished! Press enter key to exit." -ForegroundColor Green
Read-Host

 

please use CSV column as Title,Body

Remove Solution from the Central admin using Poweshell

Add-PsSnapin Microsoft.SharePoint.Powershell –ErrorAction SilentlyContinue

 

Uninstall-SPSolution –Identity bamboo.logging.v1.wsp


Start-Sleep -Seconds 60

Remove-SPSolution –Identity bamboo.logging.v1.wsp

 

Uninstall-SPSolution –Identity basicwebpartssingle.wsp


Start-Sleep -Seconds 60

Remove-SPSolution –Identity basicwebpartssingle.wsp

 

Uninstall-SPSolution –Identity contentsliderwebpart.wsp


Start-Sleep -Seconds 60

Remove-SPSolution –Identity contentsliderwebpart.wsp

 

Uninstall-SPSolution –Identity mydocstreeview.wsp


Start-Sleep -Seconds 60

Remove-SPSolution –Identity mydocstreeview.wsp

 

Uninstall-SPSolution –Identity sampletodeployapage.wsp


Start-Sleep -Seconds 60

Remove-SPSolution –Identity sampletodeployapage.wsp

 

 

 


Write-Host "Finished! Press enter key to exit." -ForegroundColor Green
Read-Host