Walkthrough: Create Form with InfoPath and use it in a SharePoint 2010 Form Library

In this walkthrough I want to show you the steps for creating a InfoPath form and use it with InfoPath Forms Services of SharePoint 2010.

1. First I downloaded a downloadable InfoPath form from Microsoft: http://office.microsoft.com/en-us/templates/new-travel-client-preference-form-TC001201306.aspx – It’s called “New travel client preference form”.

2. I create a document library where I store my InfoPath template(s).

image

image

3. Open InfoPath Designer. Open the downloaded InfoPath form (see step 1).

image

4. Go to “File” menu.

=> Click “Publish your form”

=> On the first time you will be redirected to “Publish” sub menu.

=> On “Publish” menu: Click “Quick Publish” or “SharePoint Server”

image

A wizard starts…

On the first page enter the URL of the destination site.

image

On the second page I choose “Site Content Type”. => So you can use the form on multiple lists.

image

Create new Content Type.

image

Enter the name of the Content Type.

image

Specify the location where the form template will be saved. This URL will be used for the Content Type. You know: A Content Type can have a document template setting that is used to create new items (documents) based on this Content Type. The URL will be stored in the Content Type.

image

image

Add all form columns to the resulting Content Type. This columns will be available in the SharePoint list items that represent a saved form. (Here I choose some columns for this example…)

image

Now you are almost finished. – Click “Publish”…

image

That’s the result for that:

image

 

4. Go to the browser. Open your SharePoint site.

=> Click “Libraries” in the Quick Launch.

=> Create a new Form Library like this:

image

Click “Library Settings” in the Ribbon.

image

Edit the “Advanced Settings” of the library.

=> Select the option “Allow management of content types?” May be other options. Then save.

image

Now edit the Content Types of the library on the “Library Settings” page:

image

=> Press “Add from existing site content types”.

Now choose your previously created content type “TravelPreference”. Then press “OK”.

image

Optional: Remove the standard “Form” content type from the list.

Now it looks like this:

image

 

5. In InfoPath Designer

=> Open the “File” menu.

=> Open the “Info” sub menu.

=> Click “Submit Options”

Choose “To SharePoint Library”

image

In the wizard: Specify the location for saving the filled forms:

image

Go through the next wizard page… Click “Finish”

image

=> Open the “File” menu.

=> Open the “Info” sub menu.

=> Click “Form Options”

Open “Compatibility” tab. Choose Form Type “Web Browser Form” and enter the site URL:

image

Click OK.

The “Design Checker” will open. There are some problems in the form:

image

This results in setting the form type to “Web Browser Form”: Such a form does not have some features. – Now remove the fields from the form…

Click “Refresh” in the “Design Checker” pane.

Now there’s only a warning. You can ignore this.

image

=> Open the “File” menu.

=> Open the “Info” sub menu.

Click “Quick Publish”

image

That’s it:

image

 

6. Go to the browser. Open your SharePoint site. Open your “TravelPreferences” list.

=> Click “Add new document”

NOW YOU’LL GET YOUR BROWSER FORM!!!

Fill it out!

Click “Submit” (NOT: “Save” => You can disable the “Save” button in InfoPath Designer => “File” menu => “Info” pane => “Advanced form options” => “Web Browser” tab)

After clicking “Submit” you’ll get a document in your form library:

image

This can be used with Workflows and any other SharePoint functionality…

Error while enabling Session State Service on SharePoint 2010

There are some mysterios errors on my SharePoint farm. After reading tons of log files I found some hints to problems with the “Session State Service” of SharePoint 2010.

I found this Cmdlets in PowerShell for controlling this service:

Enable-SPSessionStateService 
Disable-SPSessionStateService
Get-SPSessionStateService
Set-SPSessionStateService

I tried to enable the service with the first Cmdlet. – This was the resulting error:

image

Error message: “Microsoft SharePoint Server session state could not find the Session State Service. Contact your farm administrator.”

There is no information about this error in the internet. (Till now Smile )

MY SOLUTION FOR MY PROBLEM (may be it does not help you in your special situation. It’s “experimental”!!!):

I created a PowerShell script for re-creating the Session State Service and its Service Application.

Before you go on: Make sure, the Windows service “ASP.NET State Service” is running. (I set it to start automatically during system startup.)

This is the resulting script:

#region Check x64 host
    if( [System.IntPtr]::Size -ne 8) {
      Write-Error "Please use a x64 PowerShell host!"
      return
    }
#endregion

#region Load SharePoint SnapIn and DLL
  Remove-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
  Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
  
  #Check available SharePoint Cmdlets
  if( (Get-Command -Noun SPWeb*) -eq $null ) {
    Write-Error "SharePoint SnapIn not loaded. SharePoint cmdlets missing!"
    return
  }
#endregion

cls

$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local

$services = $farm.get_Services() #get all SharePoint services
$sessionStateService = ($services | ? { $_.TypeName -like "*session state*" } ) #find the existing Session State Service -> it was "NULL" for me!

if( $sessionStateService -eq $null ) {
    #Recreate the Service
    $newSessionStateService = New-Object Microsoft.Office.Server.Administration.SessionStateService ("", $farm)
    $newSessionStateService.Id = [System.Guid]::NewGuid()
    $newSessionStateService.Name=[String]::Empty
    $newSessionStateService.Update()
    $farm.Update()
    $newSessionStateService.Provision()
    $newSessionStateService.Name=[String]::Empty
    $newSessionStateService.Update()
}

$services = $farm.get_Services() 
$sessionStateService = ($services | ? { $_.TypeName -like "*session state*" } ) 

$servers=(Get-SPServer)

#Create service instances on all application servers of the SharePoint farm
$servers | % {
    if( $_.Role -eq "Application" ) {
      $currentSessionStateSvcOnServer = ($_.ServiceInstances | ? { $_.TypeName -like "*session state*" } ) 
      if( $currentSessionStateSvcOnServer -eq $null ) {
        #write-host $_.Name $server.Role $_.gettype().fullname
        
        #To create a service instance you must use a "protected" constructor
        [type]$t = "Microsoft.SharePoint.Administration.SPServiceInstance" -as "Type"
        $p = @( ("string" -as [Type]), ("Microsoft.SharePoint.Administration.SPServer" -as [Type]), 
                ("Microsoft.SharePoint.Administration.SPService" -as [Type]) )
        $c = $t.GetConstructor([System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Instance,$null, $p, $null)
        #these are the parameters for creating a service instance by using the protected constructor
        [Object[]]$params = @([Object]"Session State Service Instance", 
                              [Object]([Microsoft.SharePoint.Administration.SPFarm]::Local.Servers[$_.Name]), 
                              [Object]([Microsoft.SharePoint.Administration.SPFarm]::Local.Services[$sessionStateService.Id]))
        $newSvcInstance = $c.Invoke($params)
        #update & provisioning
        $newSvcInstance.Update()
        $newSvcInstance.Provision()
      }
    }
}

if( (Get-SPSessionStateService -ErrorAction SilentlyContinue) -ne $null ) {
  Write-Host "Successfull :-)" -ForegroundColor Green
} else {
  Write-Host "Failed :-(" -ForegroundColor Red
}

After that, the “Enable-SPSessionStateService” works:

image

(Spend me 1 1/2 days.)


You can use this script to delete the Session State Service, e.g. if some script parts does not work as expected. ONCE MORE: USE IT CAREFULLY AND AT YOUR OWN RISK!!!

#region Check x64 host
    if( [System.IntPtr]::Size -ne 8) {
      Write-Error "Please use a x64 PowerShell host!"
      return
    }
#endregion

#region Load SharePoint SnapIn and DLL
  Remove-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
  Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
  
  #Check available SharePoint Cmdlets
  if( (Get-Command -Noun SPWeb*) -eq $null ) {
    Write-Error "SharePoint SnapIn not loaded. SharePoint cmdlets missing!"
    return
  }
#endregion

cls

$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local

Get-SPServiceApplication | ? {$_.GetType().FullName -eq "Microsoft.Office.Server.Administration.SessionStateServiceApplication"} | Remove-SPServiceApplication 

$farm.Services | ? { $_.TypeName -like "*session state*" } | % {
  $_.Instances | % {
    $_.Delete()
  }
  $_.Delete()
}

Enumerate Sharepoint 2010 database in PowerShell

“Is there a way to enumerate all SharePoint 2010 databases in PowerShell like the page /_admin/DatabaseStatus.aspx”>/_admin/DatabaseStatus.aspx”>http://<centra_administration>/_admin/DatabaseStatus.aspx does it??”

YES!

You can use this script:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 

$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local

$type = ('Microsoft.SharePoint.Administration.SPPersistedTypeCollection`1') -as "Type"
$type = $type.MakeGenericType( ("Microsoft.SharePoint.Administration.SPDatabase"as "Type") )
$o = [Activator]::CreateInstance($type, $farm)

$’

Now the variable “o” contains all Database-Objects of your farm!

How to create an instance of an generic class in PowerShell

Sometimes you may need to create instances of generic classes in PowerShell.

Like this (in C#):

 using  System;

 using  System.Text;
 
 namespace  tmp1
 {
     class  Program 
     {
         static  void  Main(string [] args)
         {
             System.Collections.Generic.List<string> t = new  System.Collections.Generic.List<string>();
         }
     }
 }

There is a generic class used in the code above: System.Collection.Generic.List.

If you want to create an instance of this class in PowerShell you can not use this:

New-Object "System.Collections.Generic.List<string>"

You’ll receive an error!

Here is the solution: You can use the System.Type that represents other .NET types.

Use this PowerShell code snipped to create an instance of System.Collections.Generic.List<string>

$type = ("System.Collections.Generic.List"+'`'+"1") -as "Type"
$type = $type.MakeGenericType("system.string" -as "Type")
$o = [Activator]::CreateInstance($type)

“$o” will contain the created instance.

You can use this method for all other generic classes.

You may need to change the following details.

  1. In the first line of the snipped there is `+"1" This is the amount of parameters for the generic class.
  2. In the second line you have to specify the parameter types for the generic class. Separate them with “,” and cast them to System.Type by using -as “Type”.
  3. In the third line you may need to specify the constructor parameters for creating an instance of the generic class.

Here is another example:

$type = ("System.Collections.Generic.SortedList"+'`'+"2") -as "Type"
$type = $type.MakeGenericType( @( ("System.Int32" -as "Type"), ("system.string" -as "Type") ) )
$o = [Activator]::CreateInstance($type, 100)
$o.Add(1, "test")
$o

This creates an instance of System.Collections.Generic.SortedList<System.Int32, String> with a starting capacity of 10 (line 3: the second parameter of CreateInstance).

How to determine current PowerShell session is x86 or x64.

For the execution of SharePoint PowerShell scripts I need to check the execution environment of my PowerShell session to be sure that PowerShell runs as x64 application.

This was a problem because PowerShell does not provide this information itself.

BUT PowerShell is a .NET Application.

There you can check for x86 or x64 by using this static method (in C# or VB.NET):

System.IntPtr.Size

In PowerShell syntax:

[System.IntPtr]::Size

This results in “4” for x86 and “8” for x64. (Without question marks of cource. The result type is int.)

So you can use this in your PowerShell script.

Here I started 2 PowerShell sessions: one x86 and one x64. See the result of the command above:

image

(In the window title you see “Windows PowerShell (x86)” –> That’s the x86 session. The x64 session has no special window title.)

Here is a corresponding PowerShell code snipped:

#region Check x64 host
if( [System.IntPtr]::Size -ne 8) {
  Write-Error "Please use a x64 PowerShell host!"
  return
}
#endregion

Forefront Protection 2010 for SharePoint–Error “The SharePoint service is running but the Forefront VSAPI Library is not registered”

 

I got this error in the config tool of Forefront Protection 2010 for SharePoint:

“The SharePoint service is running but the Forefront VSAPI Library is not registered”

image

Here is what I did:

1. Stop running service “Microsoft Forefront Server Protection Controller”

2. Stop running service “Microsoft Forefront Server Protection Controller for SharePoint”

3. Go to your installation directory of Forefront Protection 2010 for SharePoint

4. Run from command prompt: “fsccontroller.exe /disable”

5. Run from command prompt: “fsccontroller.exe /enable”

6. Restart SharePoint Services:

    • SharePoint 2010 Administration
    • SharePoint 2010 Timer

7. Start service “Microsoft Forefront Server Protection Controller for SharePoint”

8. Start service “Microsoft Forefront Server Protection Controller”

9. That’s it. (Do a “Refresh” in the Forefront management console application.)

image

How to set the SharePoint 2010 Enterprise Search Service Application “Default Content Access Account” by using PowerShell

If you want to set the SharePoint 2010 Enterprise Search Service Application "Default Content Access Account" by using PowerShell you can use this script:

$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
$c= New-Object Microsoft.Office.Server.Search.Administration.Content($searchapp)
$c.SetDefaultGatheringAccount($crawlUser, (ConvertTo-SecureString $crawlPwd -AsPlainText -force))

($crawlUser is – of cause – the login of the account and $crawlPwd is the password in plain text)

How to set the identity of a SharePoint Windows Service, e.g. “SharePoint Server Search”

I have had the problem to set the SharePoint Sevice Account of "SharePoint Server Search" to a special user account during a automatic setup.

In other words: I want to automate this step:

Central Administration -> Security -> Configure Service Accounts

blog-201011030017

There is no PowerShell command as far as I can see.

You can use this script for this job:

  

$farm =(Get-SPFarm)

$farm.get_Services() | ? { $_.typename -ieq "SharePoint Server Search" } | % {

  $searchservice=$_

  $i = $searchservice.get_ProcessIdentity()

  $i.set_CurrentIdentityType(3)

  $i.set_Username($searchUser)

  $i.Update()

  $i.Deploy()

}

Error while trying to connect to a published Service Application in SharePoint 2010: “Unable to connect to the specified address. Verify the URL you entered and contact the service administrator for more details.”

When you receive this error

Unable to connect to the specified address. Verify the URL you entered and contact the service administrator for more details.

…and you have done the exchange of farm certificates than you forgot this step:

$consumingFarmID=<Farm GUID of the consuming farm>

$security=Get-SPTopologyServiceApplication | Get-SPServiceApplicationSecurity

$claimprovider=(Get-SPClaimProvider System).ClaimProvider

$principal=New-SPClaimsPrincipal -ClaimType "http://schemas.microsoft.com/sharepoint/2009/08/claims/farmid" -ClaimProvider $claimprovider -ClaimValue $consumingFarmID

Grant-SPObjectSecurity -Identity $security -Principal $principal -Rights "Full Control"

Get-SPTopologyServiceApplication | Set-SPServiceApplicationSecurity -ObjectSecurity $security

(Technet: http://technet.microsoft.com/en-us/library/ff700211.aspx)

See the first line of the script:

$consumingFarmID=<Farm GUID of the consuming farm>

→ Here you need to execute this PowerShell code snippet and insert the resulting GUID in the script above:

(Get-SPFarm).Id 

How to embedd compressed scripts in other PowerShell scripts

For a current project I want to create a “master script” that contains another compressed PowerShell script. This compressed script contains some informations – server names, network share names – that should be as unreadable as possible for normal users. This script is executed during user logon.

I created two scripts

  1. "compress.ps1"
  2. "execute.ps1"

The first script is used to compress a script. The second one is used to execute the compressed script.

Here is the script for compressing:

 

cls

$c = Get-Content $$ -Encoding UTF8 

$r = [string]::Join("
", $c)

$r2 = [regex]::Match($r, "(.*)(#<--)(.*)", [System.Text.RegularExpressions.RegexOptions]::Singleline)

$ms = New-Object System.IO.MemoryStream
$cs = New-Object System.IO.Compression.GZipStream($ms, [System.IO.Compression.CompressionMode]::Compress)

$sw = New-Object System.IO.StreamWriter($cs)
$sw.Write($r2.Groups[3])
$sw.Close();

$bytes = $ms.ToArray()
[System.Convert]::ToBase64String($bytes)

exit
#<--

Write-Host "This is a test"
Get-Service A*

The script produces a base64 string that contains everything behind "#<–".

For the script above the output is:

H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/In7j5DdOvlsXbb797app04/ezIsmpf9laZs37Ue/cfJ53m6/zuvLYpqnx9/6jZP/By/6yuMxAAAA

Here is the execution script that contains this base64 string that represents
the compressed script:

cls

$data = [System.Convert]::FromBase64String("H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/In7j5DdOvlsXbb797app04/ezIsmpf9laZs37Ue/cfJ53m6/zuvLYpqnx9/6jZP/By/6yuMxAAAA")

$ms = New-Object System.IO.MemoryStream
$ms.Write($data, 0, $data.Length)
$ms.Seek(0,0) | Out-Null

$cs = New-Object System.IO.Compression.GZipStream($ms, [System.IO.Compression.CompressionMode]::Decompress)
$sr = New-Object System.IO.StreamReader($cs)
$t = $sr.readtoend()

Invoke-Expression $t

This executes the following code snipped from the script above:

Write-Host "This is a test"
Get-Service A*

The output is: 

This is a test

Status   Name               DisplayName                           
------   ----               -----------                           
Stopped  AeLookupSvc        Anwendungserfahrung                   
Running  AESTFilters        Andrea ST Filters Service             
Stopped  ALG                Gatewaydienst auf Anwendungsebene     
Stopped  AppIDSvc           Anwendungsidentität                   
Stopped  Appinfo            Anwendungsinformationen               
Running  Apple Mobile De... Apple Mobile Device                   
Stopped  AppMgmt            Anwendungsverwaltung                  
Stopped  aspnet_state       ASP.NET-Zustandsdienst                
Running  AudioEndpointBu... Windows-Audio-Endpunkterstellung      
Running  AudioSrv           Windows-Audio                         
Stopped  AxInstSV           ActiveX-Installer (AxInstSV)