Thursday, April 25, 2013

Save list as a template and create list from template using PowerShell

In this post we are going to see how we can save list as a template that saves it to the list template gallery and then how we can create a new list / library from the template from the saved template earlier.

Here is a simple script that we can write in PowerShell to do that

Here we are saving shared documents library as a template and then creating a library from the saved template.


[xml]$xmlfile = Get-Content ConfigFile.xml

foreach( $sitecoll in $xmlfile.Configuration.SiteCollection) 

    $site = $sitecoll.name


$spSite= Get-SPSite $site

$web = $spSite.OpenWeb()

Write-Host -foregroundcolor yellow 'Saving list as a template.....'

$list = $web.GetList("/sites/SharePointSite/Shared Documents/")

$list.SaveAsTemplate("SDWITHDOCSET","SD with DOCSET","Shared Doc with Doc Set",1)

Write-Host -foregroundcolor Green 'Saving list as a template successfully'

Write-Host -foregroundcolor yellow 'Creating documents library from the template...'

$listTemplates = $spSite.GetCustomListTemplates($web)

$web.Lists.Add("SDFromTemplateUsingPS", "", $listTemplates["SD with DOCSET"])

Write-Host -foregroundcolor green 'Creating documents library from the template completed successfully.'

I hope this helps.

Monday, April 22, 2013

Document Set Save as a template problem


In this post, we are going to see one problem which is not been addressed properly. I think not even from Microsoft in the form of SP1 or even in the form of CU.

Problem is, you have one document library and you have document set in that library with many other documents inside each document set.

The problem is when you save a library as a template with content and when you create a document library based on the template, you can see that the document sets have come. You can even see the icon indicating that these are the document sets. However when you click on the document set, boom!! page not found.

I did not find answer anywhere. Even if you save a web as a template including content and then try to create a web based on the template, you see a strange thing. In the document library, you will find those document set icon as folders not as a icon of the document sets.

Well only thing that I observed is you go ahead and manually edit the properties of those document sets and change the content type to the document set, that will resolve the issue and when you click , you get the welcome page and documents that were there inside the document set while saving site or list as a template.

But this is not a solution. Imaging you have more than 100 libraries and each having more than 50 document sets with each almost 100 documents inside it. !!! Not sure what Microsoft is thinking on this.

If anyone has come across to this and have found proper solution, please do let us know.

Friday, April 19, 2013

Create document set programmatically


In this post we are going to see how to create a document set in SharePoint programmatically.

To start with we need to activate the feature called document set at the site collection level.

If you would like to know more about document set, then I would recommend you reading Understand Document Set to get a fair idea on what document set is and what it offers.

I have one document library with the name Shared Document. To add document set programmatically, we first need to enable the content types on this library. So go to the library settings, advance settings and enable the allow management of content types.

Now click on add from existing content types.

On the page look for document set content type and add it to the library.

So you should have something like this



Now I am showing this with an example of feature activated event. I have one feature and on activating the feature I am creating a document set programmatically.

Here is a code that you need to write.



Now if you see what we have done here is first we have taken document set content type in to a code from current web. This will not work if you have not activated document set feature at a site collection level.

Document set has properties, so we have set one property - document set description. This needs to be done with HashTable.

Then we are creating a document set by DocumentSet.Create method. To get this done you need to add reference to one DLL and for the HashTable you need to one DLL.

These are the DLLs you need to refer.

using System.Collections;
using Microsoft.Office.DocumentManagement.DocumentSets;

After creating Document Set, the important line of code to note is the ProgId of the item to be set to SharePoint.DocumentSet.

If you do not do this, then all you get is just a folder and not actual document set. You can see the difference in the icon as well as when you click on the folder.

Finally we update the item.

So let's deploy the WSP and activate the feature and see what happens.



See the icon. Icon indicates that this is the document set and not the plain folder. Click on the title.



See the description is also set. You can also set additional properties at the time of creating document set.

I hope this helps.


Thursday, April 11, 2013

Checking for validation error programatically in InfoPath

In case if you are writing a code in button click event of InfoPath page and you would like to validate the required fields input or any other validation, then here is a one liner code that you need to write.

this.errors.count 

if the count is 0 that means there is no error, else there are errors.

Happy coding!!

Thursday, April 4, 2013

Allocate tasks to users inside group in start approval process in SharePoint Designer


In this post we are going to see how we can assign tasks to the users inside start approval process on this item with these users action inside SharePoint designer.

To demonstrate I have one list with one title column and one group name column which has group as well as users settings done.




Let us open the SharePoint Designer and connect to the site. Click on list workflow and select list on which you want to add the workflow.

 Select start approval process from the actions menu.

Click on these users.



Select workflow lookup and then from current item, select the field.




Configure this workflow on start automatically on create and on edit

Now go ahead and create one item in the list.



I have added one list item with selecting engineers as a group on which approval will start.
As you can see workflow is in progress.



Click on in progress and as you can see it is assigned to the group and not to the users inside group.



So how to actually allocate tasks to the users inside the group. Well there is a small change.

Go back to the workflow in designer. Select the step and click on advanced properties from ribbon.



And change the expand group to yes.

Publish the workflow now.

Create one more item in the list, click on the in progress status of the workflow. and there you go.



Task is now allocated to the user inside the group. As this approval is set as a serial, go ahead and approve this. You will see the next task allocated to the next user in the group.



I hope this helps.

Tuesday, April 2, 2013

Display list item in edit mode in InfoPath Form Web Part


In this post we are going to see how we can open a list item in edit mode in InfoPath Form Web Part. This initially seems to be an easy job. But it is not.

I really had to invest a lot of time figuring out this solution. What we want to achieve is actually a list form customized using InfoPath by using customize this form in InfoPath ribbon option and then by using the InfoPath form web part we display the form of the list.
I have a list called orders.



and I have used customized the form in InfoPath option from ribbon so that new item, edit item and view item opens up in InfoPath form and not the list form.





If you select the list item and click on edit the item, you can see item is opened in the InfoPath.



Now here comes a tough part. Assume that you want to have another web parts on the page and on the same web part page you would like to have this order list item new entry form to be displayed.

Well, this is not as tough as you think. you can simple add the InfoPath form web part and then set the list name to order and you are good to go.



Once you click on ok, you can see the form has been added on the web part.



Now comes the challenging part, how would you open any list item in the InfoPath Form Web Part. Now again this seems an easy job but it is not as I stated earlier.

I had to go through lot of blog posts and articles. Many of them mentioned that you need to have source list and then bind the connection between InfoPath Form web part and source list so that when you select the source list item, that item gets populated in the InfoPath Form Web Part.

However this is not the solution that we want. We cannot add a source list in our case, orders to the same page and connect these two web part.

There can be a scenario where you are just passing ID of the list item from another page and based on the query string you want to show the edit form in the InfoPath form web part.

So here is a solution that I came out with. But yes if you do not have code option available to you, then you have to go with the connect web part approach. But for that you will have a list also displayed on the same web part page.

All we need to do is create one simple web part (not the visual web part) . add a reference to the c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.Office.Server.dll DLL.

protected override void CreateChildControls()
        {
            BrowserFormWebPart web = new BrowserFormWebPart();

            web.FormLocation = @"{siteURL}/Lists/Orders/Item/editifs.aspx?List={list GUID}&LItemID={ITEMID}&Source={siteURL}%2FLists%2FOrders%2FAllItems%2Easpx&ContentTypeId=0x01005AFFDCC34451FF46B97FDD902EA436AF";
            this.Controls.Add(web);

        }

Just replace the List GUID, site URL and ID with the actual list GUID, site URL and list item ID. you can get these through query string or as per your requirement.

Deploy the web part and you are good to go. Now you have a InfoPath Form web part which shows list item in edit mode. 



Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com