Wednesday, September 26, 2012

Upload content type template through PowerShell

In my current project, we make use of numerous different Word templates. To make template a part of the automated build process, I made a PowerShell script that

  1. Loop through a folder on the disk
  2. Find subfolder, named in a given way
  3. Find the template
  4. Finds the content type in SharePoint
  5. Upload the template to the content type resource folder http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontenttype.resourcefolder.aspx
  6. Save the content type

And the content type is ready to get published.

PowerShell script:

1 Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction:SilentlyContinue | Out-Null
2 $url = "http://win-l2sfc3oetnn/sites/doctemp/" #ContentTypeHub Url
3 $templateFolderPath = "C:\Projects\demos\TemplateDemo"
4 $filter = "*.dotx", "*.potx", "*.vst", "*.xltx"
5
6 $contentTypeFolders = ls $templateFolderPath | where {$_.PsIsContainer}
7 $templateCount = (ls $templateFolderPath -Include $filter -Recurse | where {!$_.PsIsContainer}).Count
8 $i = 1
9 $fileMode = [System.IO.FileMode]::Open
10
11 $contentTypeFolders | % {
12 $path = $_.FullName
13 $foldername = $_.Name
14 $ctindex = $foldername.LastIndexOf("0x")
15
16 if($ctindex -gt 0) {
17
18 $contenttypeid = $foldername.Substring($ctindex, $foldername.Length - $ctindex)
19 $spcontenttypeid = New-Object Microsoft.SharePoint.SPContentTypeId $contenttypeid
20
21 ls -Path $path | % {
22 #Need to open a new spweb object, to "refresh" the current content type object, to aviod database update conflict
23 $web = Get-SPWeb $url
24 $spcontenttype = $web.ContentTypes[$spcontenttypeid]
25 $fileFullname = $_.FullName
26 $filename = $_.Name
27
28 Write-Progress -PercentComplete (($i / $templateCount) * 100) -Activity "Uploading templates" -Status "$filename on content type $($spcontenttype.Name)"
29
30 $i++
31 try{
32 #open the document template, as a filestream
33 $fileStream = New-Object "System.IO.FileStream" -ArgumentList $fileFullname, $fileMode
34 #add the file (SPFile) to the content type resource folder http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontenttype.resourcefolder.aspx
35 $spcontenttype.ResourceFolder.Files.Add($filename, $fileStream, $true) | Out-Null
36 $fileStream.Close()
37 #set the current document template file to be the document tmeplate
38 $spcontenttype.DocumentTemplate = $filename
39 #update the content type
40 $spcontenttype.Update($true)
41 }
42 finally{
43 $fileStream.Close()
44 }
45 }
46 }
47 }

Download link

Wednesday, September 19, 2012

QuickPart values ​​do not update with a blank Word templates

In my current project, we make use of numerous different Word templates, but the basic for them all is they have a header and footer and they are the same. Header and footer containing quick-party showing SharePoint metadata, for e.g. status of the document. Of cause the values must be updated if the user changes them either from Word (document information panel) or from the SharePoint UI. We made a lot of templates, from meeting minutes to a letter template and all the values ​​from the quick-party fields updated fine but our most simple Word template that only contain, header and footer, did not work. After a very long search we came to the Word template must include a text and a space is enough.

This will not work:1

This will work:2

Wednesday, September 12, 2012

Custom document infomartion panel, InfoPath form as letter selector

I got a request from a customer, where they needed to add a custom address field to the head in a Word template. The address list need to come from a single source and easy to by update.

The tasks look like this:

  1. Create the custom address list in SharePoint. Need a multiline field, where the address with line breaks can be added.
  2. Content type with a Word template and custom information panel “InfoPath form”
  3. The InfoPath form
  4. Configure the document library
  5. The Word template
  6. Upload the Word template
  7. Test


Creating the list for the address

Create a custom list.

1.1

Adding the multiline field.

1.2

Add value to the list.

1.3

Creating the content type

I call my content type “Letter”.

2.1

Add multiline text field, to the content type. In my demo i use the build in field Address.

2.2

When done, the content type look like this. Click on the document infomartion panel settings link.

2.3

Clikc on the "Create a new custom template” link.

2.4

InfoPath view

When InfoPath is finish loading, click on the “show field” button in the ribbon, “manage data connection” and then “Add…”.

3.1

Datasource wizzard

Follow the data connection wizzard.

3.2.1

3.2.2

The locatition is the url to the SharePoint site where the custom address list is created.

3.2.3

3.2.4

3.2.5

3.2.6

3.2.7

The done, the new data connection is add to the data connection lists.

3.2.8

Change the control

Right click on the Address field textbox, select “Change Control” and select “Drop-Down List Box”.

3.3

Change the control properties

Click on the Address field and select Drop-Down List Box Properties.

3.4.1

Change the properties to look like this.

3.4.2

Change the design a bit

Remove the line break after the first horizontal region.

3.5

Publish my form

Click on the Quick Publish button, in the right top corner.

3.7.1

InfoPath will akes if you want to save the InfoPath template, before publish it.

3.7.2

Configure and  add content type to document library

On the document library, go to the document library settings page and under the “Advanced settings” page, change the setting “Allow management of content types” to true.

4.1.1

And the “Letter” content type.

4.1.2

Create the Word template

Go back to the document library and create a document based on the content type. This is the easyed way to create a template with the proper field add to the Word template.

5.1

Then Word is finish loading, go to the backstage, by clicking on the “file” icon, then click on the “properties” and select “Show Document Panel”.

5.2

Go back to Word.

In my demo, i edit the header. Go the Ribbon, under the “Insert” tab, select “Quick Part”, “Document Property” and then the “Address” field.

5.3

My template ends up looking like this.

5.4

Save the document as a “dotx” file (Word template file).

5.5

Upload the template

Find the “Letter” content type, select the “Advanced settings”.

6.1

Upload the template og click on the “OK” button.

6.2

The test

Go back to the document libary, click on the “Letter” content type in the “new document” button in the Ribbon.

5.1

Select the address in the document infomation panel.

7.1

And we are done Smiley

Wednesday, September 5, 2012

A great migration tool

On my current project, we needed that to merger quite a lot of documents from one SharePoint to another SharePoint. To begin with, we expected that the project should do the merger of the documents and therefore we should spend a lot of time on it. We discuss it in the project group and came up with some solution proposal, which ranged from console application to PowerShell. However, we kept coming back to a problem. That we in the project did not know the structure that we had to migrate documents into, that was only our customer who is familiar structure. Of course, we could talk to the customer about how we should save every single document. After some discussion back and forth, we end up finally but not a particularly good solution. So back to the drawing board. Now, if we could create a program that could even be able to use and even stand to migrate documents, it would be the best solution. After some discussion back and forth, we end up finally, not a particularly good solution. So back to the drawing board. Now, if we could make a program that the customer could use and themselves migrate the documents, it would be the best solution. We estimate tasks and found out, if we create the program, it would take a very long time to develop. Therefore, we began to search for a 3-party tool. We found a few tools, but we found a tool from Share Gate.

ShareGate tool had several advantages, which we need, as they highlight on their website:

  • Drag & drop
  • Migrate SharePoint list items and documents
  • Preserve version history
  • Preserve created and modified information
  • Assign new content types
  • Field validation

But the most important of all, an interface that is easy and inviting, so that our customer does not need constant support to them and it's ShareGates "Copy SharePoint Content" tool.

Product Information:
Company: http://en.share-gate.com/
Product: http://en.share-gate.com/sharepoint-tools/copy-move-sharepoint-list-items-documents-with-metadata-and-version-history
Download: http://en.share-gate.com/download