User Word Automation Service using PowerShell

With PowerShell also possible to use Word Automation Service to convert documents to pdf, xps, etc formats.

Execute below script in PowerShell on your SharePoint Server. Don’t forgot to make necessary changes in script as per your environment. Also Word Automation Service should be in running state.


$jobSettings = New-Object Microsoft.Office.Word.Server.Conversions.ConversionJobSettings

$jobSettings.OutputFormat = "PDF"

$job = New-Object Microsoft.Office.Word.Server.Conversions.ConversionJob("Var Word Automation Service", $jobSettings)

$job.UserToken = (Get-SPWeb http://VarSiteName).CurrentUser.UserToken

$job.AddFile("http://VarSiteName/Document%20Library/VarDocName.docx", "http://VarSiteName/Document%20Library/VarDocName.pdf")


For UI based solution check this link: SharePoint Document Converter

Play with SharePoint 2010 List using PowerShell

In this post, we will learn how to do basic operations on SharePoint list using PowerShell.

Lets start with List creation

$spAssigment = Start-SPAssignment    #Use to dispose SPWeb safely
$spWeb = Get-SPWeb ServerURL -AssignmentCollection $spAssignment     #Get SPWeb Instance
$spWeb.ListTemplates | Select Name, Description    #Display List of Templates Available
$spTemplate = $spWeb.ListTemplates["Custom List"]     #Create SPTemplate instance of Type Custom List
$spWeb.Lists.Add("List Title", "Description", $spTemplate)     #Add list to site

Add list to quick launch and fields to list

$spList = $spWeb.Lists["List Title"]    #Get list instance
$spList.OnQuickLaunch = $True    #Set true to display in Quick Launch
$spList.Update()    #Update list to reflect changes in site
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Text      #Get Field type to create
$spList.Fields.Add("My Column", $spFieldType, $false)      #Add new field to list

Create View and set fields for view

$spViewQuery = “<OrderBy><FieldRef Name=”"Modified”" Ascending=”"False”" /></OrderBy>”
$spViewFields = New-Object System.Collections.Specialized.StringCollection    #Create string collection object
$spViewFields.Add("Title")    #Add columns
$spViewFields.Add("My Column")
$spViewName = "My View"        #Set view name
$spListView = $spList.Views.Add($spViewName, $spViewFields, $spViewQuery, 100, $True, $False, “HTML”, $False)    #Add view to list
$spListView.DefaultView = $True       #Set view as default
$spListView.Update()    #Update view to reflect changes in site

Add few List Items

$spListItem = $spList.Items.Add()    #Add new list item object
$spListItem["Title"] = "First Item"    #Set field value
$spListItem["My Column"] = "My Text for first item"
$spListItem.Update()    #Update list item object to reflect changes in site

$spListItem = $spList.Items.Add()
$spListItem["Title"] = "Second Item"
$spListItem["My Column"] = "My Text for second item"

Update List Items

$spListItems = $spList.Items    #Get list item collection
$spListItemsCount = $spListItems.Count    #Get list items total count
for($Counter = $spListItemsCount-1; $Counter -ge 0; $Counter--)
    $spListItem = $spListItems[$Counter]    #Get list item via index
    $spListItem["Title"] = "Updated Title" + $Counter
    $spListItem["My Column"] = "My Column" + $Counter

Delete List Items

for($Counter = $spListItemsCount-1; $Counter -ge 0; $Counter--)
    $spList.Items[$Counter].Delete()    #Delete list item

Delete List


Finally Dispose web object

Stop-SPAssignment $spAssignment

Happy Reading!!!

Extract WSP Solution files from SharePoint 2010 Farm

Before deploying updated WSP solution to SharePoint farm, administrators wish to take backup of current deployed solution in production servers just to stay on safe side. There is no option in CA to achieve this. Here is the script which gives us power to do.

(Get-SPFarm).Solutions | ForEach-Object{$var = (Get-Location).Path + "\" + $_.Name; $_.SolutionFile.SaveAs($var)}

To download single file use below script (Replace the YourSolutionFileName with actual WSP file name like abc.wsp):

$var = (Get-Location).Path + "\" + "YourSolutionFileName";(Get-SPFarm).Solutions["YourSolutionFileName"].SolutionFile.SaveAs($var)

$var = (Get-Location).Path + “\” + “YourSolutionFileName”; (Get-SPFarm).Solutions[“YourSolutionFileName”].SolutionFile.SaveAs($var)

SharePoint 2010 File Storage System

SharePoint provide us a nice UI to store and read documents. But what happen behind the scene. Where it store all the files???

File System or Content Database

When we open document library in windows explorer view, SharePoint allow us to delete/update/add files as we do in the normal file system. So, what does it mean that files are stored in a physical location.

Nope. Not with SharePoint. It keep all the documents in a database. Ooooh database!!!

Doesn’t sound good to keep the documents in a database. What if my file size is big. Will it not slow down the performance as SQL BLOB operation are costly than accessing file from a physical path. Even SQL Express editions has limitation on database size (approx 10GB i think). Do to SQL storage, SharePoint allow file upload of max 2GB.

Cons with Content Database

– Costly SQL operations

– Limitation on file size

– Huge database size due to big files

Pros with Content Database

– Easy backup/restore process. As all data in one single file

With release of SharePoint, we have the option to configure storage of SQL Binary Large OBjects (BLOB) to physical file system. In SQL Server 2008 (including express 🙂 ), Microsoft provided an add-on feature pack called Remote Blob Storage (RBS) used for storage of BLOB to physical location.

Lets start how we can enjoy RBS feature with SharePoint 2010. Below post will explain how to configure RBS for SharePoint 2010.

Step 1. Select the Content database for RBS. (It is possible to limit the RBS per Web Application level)

a. Open Central Administration Site -> Application Management -> Manage Content Databases

b. Select Web Application for which you want to enable RBS. Note down the content database name for selected Web Application.

Step 2. Enable file stream on SQL Server 2008/R2 (including express edition)

a. Open SQL Server Configuration Manager -> SQL Server Services

b. Right click SQL instance on which SharePoint is running, and then select properties.

c. Select FILESTREAM tab -> checkbox Enable FILESTREAM for Transact-SQL access.

d. Select other checkboxes as per your requirement and click Apply -> Ok.

e. Open SQL Server Management Studio. Execute following query for Web Application Database as selected in step 1.

EXEC sp_configure filestream_access_level, 2

Step 3. Provision a BLOB store

a. Open SQL SERVER Management Studio. Execute below queries for Web Application Database as selected in step 1. Note: Replace WSS_Content as per step 1.

use [WSS_Content]
if not exists 
(select * from sys.symmetric_keys 
where name = N'##MS_DatabaseMasterKey##')
create master key encryption by password = N'Admin Key Password !2#4'
use [WSS_Content]
if not exists 
(select groupname from sysfilegroups 
where groupname=N'RBSFilestreamProvider')
alter database [WSS_Content]
add filegroup RBSFilestreamProvider contains filestream
use [WSS_Content] 
alter database [WSS_Content]
 add file (name = RBSFilestreamFile, filename = 
to filegroup RBSFilestreamProvider

Step 4. Install RBS.

a. Download installation package using links: X64 Package X86 Package IA64 Package

b. Install RBS client library on the web server. Note: Replace WSS_Content as per step 1 and DBInstanceName as per your SharePoint environment. Locate cmd prompt to download folder and execute following command.

msiexec /qn /lvx* rbs_install_log.txt /i RBS-x64.msi TRUSTSERVERCERTIFICATE=true FILEGROUP=PRIMARY DBNAME="WSS_Content" DBINSTANCE="DBInstanceName" FILESTREAMFILEGROUP=RBSFilestreamProvider FILESTREAMSTORENAME=FilestreamProvider_1

Step 5. Enable RBS

a. Open SharePoint 2010 Management Shell and execute following commands. Note: Replace WSS_Content as per step 1.

$cdb = Get-SPContentDatabase <ContentDatabaseName>
$rbss = $cdb.RemoteBlobStorageSettings

Step 6. Test RBS Installation
a. Browse to RBS directory store and confirm folder is empty.
b. Upload a file to SharePoint Web Application (of atleast size 100 Kb).
c. Browse to the file in the RBS directory store and open the file with the most recent modified date. This should be the file you have uploaded.

Cheers !!!

SharePoint 2010 complete farm installation without domain account

Usually while SharePoint installation we have to select between Standalone and Farm installation. For Standalone installation we don’t need a domain account. It is automatically configured to use local service accounts. But if we have installed SharePoint with Farm Complete setting and still want to use the local service account during for SharePoint configuration.

In normal scenario, we get the below error message while running SharePoint configuration Wizard first time.

The specified user xxx is a local account. Local accounts should only be used in stand alone mode.

Now comes the tricky part. How to bypass this step. This can be achieved by performing below steps:

1. Run SharePoint PowerShell with administrator privileges

2. Run command: New-SPConfigurationDatabase (Provide all the necessary parameters)

3. Make sure provided service account is member of local Administrator group.

4. Now run the SharePoint configuration wizard again. You won’t be asked to enter the domain account.


Amazing Uncommon Facts

Q:> Why Microsoft named ISAPI folder as _vti_bin in SharePoint?
Ans:> Microsoft acquired software company called Vermeer Technologies Incorporated which is founded in 1994 and has only one product Front Page.
The _vti_bin folder was used to store the FrontPage server-side extensions. In SharePoint same is used to store the Web Services.

Q:> Why SP workflow doesn’t start automatically when item is created via object model?

Ans:> Many times we have requirement to create a task via workflow or object model and set the list setting to automatically trigger the workflow when item is created. In some scenarios we notice that item is created successfully but workflow doesn’t start. But if we create item via UI then workflow works perfectly. Here is a small tip to debug this issue.

If item is created via system account workflow will not trigger automatically. Same applies to event handlers.

Working with SharePoint Client Object Model

Till SharePoint 2007 we are familiar with SharePoint server object model which is very powerful and allow us to do everything if we have permission to access server physically. For scenarios where our applications can’t be deployed to SharePoint (SP) server but want to use SharePoint as a data source. We go with SharePoint OOB web services (which are limited in functionality) or end up with creating custom services to meet business requirements. In case of custom services, still we need SharePoint server access to deploy our services. Think about scenarios, where it’s impossible to have server access like SharePoint on cloud server (may be one possible scenario). Providing someone access to SharePoint root server on cloud is very risky. Or think about some other department want to use our SharePoint instance. We are forced not to let them deploy there applications on SharePoint server. What could be the possible solution to this? “SharePoint Client Object Model” is the shining star comes with SharePoint 2010.

SharePoint exposes 3 object models:

  1. Managed
  2. Silverlight
  3. ECMAScript (Standardized scripting language like JavaScript or JScript)

Before we go to what each object model do let’s see how client object model works.

When we make a call to SharePoint via client object model,  each client interact with server using service call means client object model makes call to SharePoint service Client.svc (deployed on SharePoint server and act as an interface between Client object model and SharePoint). I hope we all know where SharePoint services are deployed. If don’t know problem you can find it under ISAPI folder of 14 hive. Below is the architectural diagram explains how client access SP server:

Brief about each client object model:

Managed – used to develop custom .NET applications like service, windows applications (WPF), or console applications.

Silverlight – As name signifies it is used by Silverlight applications.

ECMAScript – These are little different than other client object models. These are used inside applications which are hosted inside SharePoint application itself.  In simple terms, cross side scripting is not allowed via ECMAScripts. These scripts should be used on SharePoint pages to access hosting server resources not others. We can use JavaScript or Jscript to achieve same.

Let’s go further deeper. Now we will create applications using each object model to perform CRUD operation on a list.

CRUD operation on a list using Managed Object Model

Step 1: Create a Console application

  1. Open Visual Studio 2010 (Run as Administrator).
  2. Select Windows Console application template (Check you are using .NET Framework 3.5).
  3. Name the project as you like and click ok button.

Step 2: Add SharePoint Client Object Model references

  1. Copy Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll from SharePoint server ISAPI folder (inside 14 hive) to your project folder.
  2. Right click on project and select Add Reference.
  3. On Add Reference window, select Browse tab and navigate to location where you have copied DLL files. Add both files to project.

Step 3: Write necessary code to do CRUD operation

Copy paste code from attached file to Program.cs file and run your project. (Don’t forget to replace siteURL variable with your server name).


This is just a simple example to start with. We can do lot more with Managed client object model.

CRUD operation on a list using Silverlight Object Model

Silverlight Object Model is slightly different in a way we make call to SharePoint from managed object model. Most of the calls made via Silverlight object model are asynchronous; these calls should not update the current running UI thread. If try to do we will get exception.

Another thing before start is we must place the ClientAccessPolicy.xml file in the SharePoint web application virtual directory. Here is content of xml file:

<?xml version=”1.0″ encoding=”utf-8″ ?>




<allow-from http-request-headers=”*”>

<domain uri=”*” />



<resource path=”/” include-subpaths=”true” />





Let’s start with our Silverlight application.

Step 1: Create a Silverlight project

  1. Open Visual Studio 2010 (Run as Administrator).
  2. Select Silverlight application template (Check you are using .NET Framework 3.5).
  3. Name the project as you like and click ok button.

Step 2: Add SharePoint Silverlight Client Object Model references

  1. Copy Microsoft.SharePoint.Client.Silverlight.dll and Microsoft.SharePoint.Client.Silverlight.Runtime.dll from SharePoint server ISAPI folder (inside 14 hive) to your project folder.
  2. Right click on project and select Add Reference.
  3. On Add Reference window, select Browse tab and navigate to location where you have copied DLL files. Add both files to project.

Step 3: Write necessary code to do CRUD operation


Copy paste code from attached file to MainPage.xaml.cs file and run your project. (Don’t forget to replace siteURL variable with your server name).

CRUD operation on a list using JavaScript Object Model

JavaScript object model only works if we use inside current server context I mean from WebParts or Application pages etc. JavaScript object model can’t be used to have cross-site access. This behavior makes it different from above object models. We can also use jQuery in place of JavaScript.

Step 1: Create an empty SharePoint project

  1. Open Visual Studio 2010 (Run as Administrator).
  2. Select SharePoint Empty Solution template (Check you are using .NET Framework 3.5).
  3. Name the project as you like and click ok button.

Step 2: Add Application to project

  1. Right click on project and select Add New Item.
  2. Select Application Page and click ok.

Step 3: Write necessary code to do CRUD operation


  1. Copy paste code from attached file to ContentPlaceHolderID=”PlaceHolderMain” In JavaScriptOM.ascx file.
  2. Press F5 to run project. Modify url to view application page like Http://servername/_Layouts/ApplicationFolderName/ApplicationPageName.aspx
  3. Test each CRUD operation

Thant’s all with the SharePoint Client Object Model. More details can be found on Microsoft MSDN. It’s very well documented there.

Links worth looking at:

Client Object Mode

JS Client Object Model and UI Enhancements

%d bloggers like this: