To install the project just by deploying the solution. The SharePoint feature will automatically be activated in the Central Administration Web Application without need of further actions.
In the “System Settings” section of the CA you’ll find a new link:
Just click it and you will get this:
The SharePoint PowerShell SnapIn is loaded automatically so that you can start immediately to execute SharePoint Cmdlets!
Here is an example:
“Get-SPSite”
Or this one:
Request for value for missing parameter “Path” of Cmdlet “Backup-SPSite”
Final output:
The project is “BETA”. I need your help to improve the project. Please feel free to test it or to extend it. But if you do so please send me your results!
This time a tiny neat walkthrough of how to add an Event Receiver at runtime in SharePoint 2010.
Let’s say you have a SharePoint site that your colleagues already use. In this site you have an existing list. Now you want to add some automation to this existing list. – You cannot deploy the list as List Definition w/ List Instance again in a VS 2010 SharePoint project, because the list exists and the data must not be touched.
One solution is to add an List Event Receiver that is contained in a VS2010 solution package.
1. You create a Empty SharePoint 2010 project in Visual Studio 2010.
2. Now you add an “Event Receiver” project item
3. Now you add the events you want to handle. Select “List Item Events” and “Custom List”.
4. Implement some functionality in the newly created Event Receiver class.
5. Now create or open an Feature Event Receiver for the SharePoint feature that will configure the event receiver. – You have to create a new feature or use an existing feature… If you create a new feature event receiver you have to uncomment the methods “FeatureActivated” and “FeatureDeactivating”.
6. Add this code to the “FeatureActivated” method:
try {
SPWeb web = (SPWeb)properties.Feature.Parent;
SPList l = web.Lists["My SharePoint List"];
if( l != null )
{
bool found = false;
foreach( SPEventReceiverDefinition er in l.EventReceivers )
{
if( er.Class == "Full.Namespace.Qualified.Class.Name.Of.Your.Event.Receiver.Class") {
found = true;
break;
}
}
if( !found )
{
SPEventReceiverDefinition newERD = l.EventReceivers.Add();
//the next line is only valid if the event receiver class is in the same assembly as the feature event receiver!!!
This is not easy. It took me some days. – Now it works!
Here I’ll show you how to create a List Form for a SharePoint List using InfoPath 2010. Therefore you need SharePoint 2010 Server with Enterprise CALs! – With InfoPath Forms Services it’s possible to render List Forms (“New”, “Edit” and “Display” forms) as browser enabled InfoPath forms. This is really, really cool!
9. Open the site in the browser. Navigate to the “Test List 1”. Select “Edit in SharePoint Designer” from the “Site Actions” menu.
10. In SharePoint Designer 2010 open the “Lists and Libraries” from the “Navigation” pane and open the “Test List 1”.
In the Ribbon you’ll see the command “Design Forms in InfoPath”.
Click the command button and select “List Item” in the drop down menu.
Now InfoPath Designer 2010 opens…
11. In InfoPath Designer 2010 modify the form.
(I will not modify it.)
Press the “Publish” button on the Shortcut Menu:
12. In SharePoint Designer 2010 choose “All Files” from the “Navigation” pane. Click “Lists”.
Click “TestList1”.
Click “Item”.
Right-click “template.xsn”. Select “Properties…” from the context menu.
Select the “Location” from the properties dialog.
Copy the URL and open it in a new browser window. You’ll get a “File Download” dialog.
Save the file to a temporary location, e.g. to the Desktop.
13. Add a new “Module” project item and name it “Form”.
Open a Windows Explorer. Navigate to the Desktop. Drag the file “template.xsn” from the Desktop into the “Form” module inside the Visual Studio project.
Modify the “Elements.xml” file of the “Form” module.
14. Add a new “Module” project item and name it “FormPages”.
Into the “Elements.xml” file of this module enter the following code:
During deployment this creates the form aspx pages that will host the InfoPath Web Part that renders the form at runtime.
In the XML code there are several references to the destination list. All of them have to be set to “Lists/TestList1”. All the code needs to remain unmodified. Don’t change “WPTypeId” attributes! “{b1dc92e2-8558-f555-ae81-35ed9ddf1644}” is the identifier for InfoPath Render Web Part.
BUT change the “ID” attributes of the three “WebPart” tags! Use the “Create GUID” tool from the “Tools” menu of Visual Studio 2010.
15. Now open the “Schema.xml” file of the List Definition project item of “Test List 1”. Add the following code at the before the closing tag of our list content type.
This will define the “template.xsn” to be a “InfoPath Forms Services” file ().
17. Deploy the project.
18. Open the browser and navigate to the “Test List 1”.
Click “Add new item”…
19. HERE WE ARE!
But if you enter values and try to save you get this error:
This is caused by the missing “Title” field inside the form. This field is marked as mandatory so you need to define it or to remove it from the Content Type and modify the form.
We will do the last one now.
20. In Visual Studio modify the “schema.xml” file of the “Test List 1”. Insert the marked line of code inside the “FieldsRefs” tag of the content type with id starting “0x01…”.
Now search for “LinkTitleNoMenu” in the schema.xml file. Replace this “view field reference” with “Edit”.
Now search for “LinkTitle” and replace it with “Edit” too.
21. In Visual Studio look for “template.xsn” in “Form” path in the Solution Explorer. Right-Click the project item and select “Open Containing Folder”.
22. In Windows Explorer right-click the file “template.xsn” and click “Design”. The InfoPath 2010 Designer will open.
23. In InfoPath 2010 you’ll see that “Title” cannot be removed. To remove it you would need the edit the xsn file with Notepad: XSN is a CAB file. So you can rename it to cab, extract it’s content, modify the “manifest.xfs” file and compress the folder as CAB file named “template.xsn”… (If you need assistance on this please post as comment.)
In the “Fields” pane select “Title”.
(This is the “advanced view” !)
Click the arrow behind the element. Click “Properties” in the context menu.
Uncheck “Cannot be blank”. Click the “OK” button. Save the file. Don’t press “Publish” on the upcoming dialog. Just press “Save”!
Quit InfoPath.
Deploy the project.
24. In Internet Explorer refresh you site and add a new item to the “Test List 1”.
You’ll see: the “Title” column is gone. Now we have a “Edit item” column on the list view.
Now add an item.
Save it.
25. HERE WE ARE AGAIN!
It works for me now!
Just for testing purpose select the list item and click “View Item” on the Ribbon.
You will see the list item in a read-only InfoPath browser form!! – If you click “Edit” on the Ribbon, you’ll be able to edit the item.
In this walkthrough I want to show you how to create a Sequential Workflow with Visual Studio 2010 for use in SharePoint 2010. – I will show how to create a custom Task Form for interaction with users. The Task form will be a native SharePoint list form. No InfoPath. There are many InfoPath samples out there but they cannot be used on a SharePoint Foundation 2010 platform. But workflows can be used on SharePoint Foundation 2010 too!
To reproduce the following steps you need to create a SharePoint site. – In the walkthrough I’ll use a Site Collection located at “http://sharepoint.local/sites/workflow”.
The “TemplateType” attribute represents the “Workflow History” list template. It resists on a SharePoint feature with ID “00BFEA71-4EA5-48D4-A4AD-305CF7030140”. It’s a native SharePoint feature.
You can add the attibute “Hidden” to the “ListInstance” tag and set it’s value to “TRUE” to hide the list as it’s done by SharePoint by default for this list. In this case you should also change “OnQuickLaunch” to “FALSE”. For my demo purpose I want to have “Workflow History” visible and on the Quick Launch bar.
5. Now we will create all tools we need for a “Workflow 1”. (May be I’ll create more workflow demos later. So it’s number 1.)
6. We create the SharePoint fields for “Workflow 1”. Therefore we create another “Empty Element” project item named “Workflow 1 Fields”.
I’ll create 3 fields for use in the Task Form we will create: Test1, Test2, Test3. They are all fields of type Text.
This specifies there to store the “Task1Form.aspx” file in the site structure.
9. In the next step we create the task list that will contain our workflow tasks.
First create a “List Definition” project item named “Workflow 1 Tasks”.
Use “Workflow 1 Tasks” as name of the list definition and “Tasks” as base type. Check “Add a list instance…”.
10. Now open “Elements.xml” of the new list definition project item.
We need to change the identifier of the list type we create! – It must be “107”. This is the list template ID for workflow tasks lists in SharePoint. The workflow designer will search for a list with this type inside the site where a new workflow will be created.
Here is the content of “Elements.xml” after our modification:
You see the “FormUrls” tag? Inside this tag we specify our custom form template we created before.
The new content type is derived from the “Workflow Task” content type 0x010801. – In the “FieldRefs” section we add our fields we need inside the workflow.
13. Now we need to add the field definitions of our custom fields to the “Schema.xml”. Copy them from the “Elements.xml” file of project item “Workflow 1 Fields” into the “Fields” tag of “Schema.xml”:
Furthermore you need to add a field definition for the field “Completed”:
Check “Do not prompt…”. Press “Resolve Automatically” – it’s your only option .
15. Have a look into the site using the browser. – We will test our “Edit” form. Remember that we did not specify special “New” form or “Display” form. This you could do the same way as you created the “Edit” form.
We see our lists in the Quick Launch.
Open the “Workflow 1 Tasks” list. On the Ribbon open the “Items” tab. We see our Content Type in the New Item submenu:
Create an item of this type. You see a standard “New” form and on it you see our three “Test” fields:
Enter some data and press “Save”.
Open the item in “Edit” form. Now you should see our custom list form.
If you click “Save As Draft” your changes will be stored in the task item. If you click “Complete Task” two item fields will be changed in addition to the changes made in the form: It sets “Status” to “Tasks_Completed” and “% Complete” to “100”. You can see this in the Code Behind file of the list form.
Test all buttons.
After “Complete Task”:
You see: “% Complete” is set to “100 %”.
So far our projects works as expected.
See Part 2 for the next steps… There I will show you how to create a simple Sequential Workflow that uses our Task Form.
In this walkthrough I want to show you how to create a Sequential Workflow with Visual Studio 2010 for use in SharePoint 2010. – I will show how to create a custom Task Form for interaction with users. The Task form will be a native SharePoint list form. No InfoPath. There are many InfoPath samples out there but they cannot be used on a SharePoint Foundation 2010 platform. But workflows can be used on SharePoint Foundation 2010 too!
To reproduce the following steps you need to create a SharePoint site. – In the walkthrough I’ll use a Site Collection located at “http://sharepoint.local/sites/workflow”.
16. Now we need to add a list that we use to connect a workflow with. We create a “List Definition” project item named “Workflow 1 Host List”.
We change the “Elements.xml” file of the List Instance project item.
We change the Title, URL and Description.
17. Deploy the project.
18. Now we start creating a Sequential Workflow.
Create a “Sequential Workflow” project item named “Workflow 1”.
Create the workflow as “List Workflow”.
You will see all the lists we created in the previous steps. Remember: You have to (successfully) deploy the project before you can use the lists that will be created during deployment. – Without previous deployment the wizard may fail before you see the following dialog page.
In the first dropdown list choose “Workflow 1 Host List”. All other lists should be selected automatically because there is only one usable list of every needed type.
Select all “Workflow start” options: “Manually”, “Item Created”, “Item Updated”.
You get:
19. We will add a “Create Task With Content Type” workflow activity that will use our “Workflow 1 Task 1” we created in the steps 1 to 15 (blog post “part 1”).
Open the “Toolbox” pane and drag “CreateTaskWithContentType” into the Workflow Designer.
After you did this you get:
Now we need to configure the workflow activity in the “Properties” pane.
Set “correlationToken” to “task1” and the sub element of “correlationToken” named “OwnerActivityName” to “Workflow_1”.
Now we need to create code behind properties for some activity properties. As example I’ll show how to create a code behind property for “ContentTypeId”.
Click on the Button “…” at the activity property edit box:
In the dialog select the tab “Bind to a new member”. Enter the name “New member name” and select the “Create Property” radio button. Press “OK”.
Here are the property bindings you need to create:
“ContentTypeId” = “task1ContentTypeId”
“TaskId” = “task1Guid”
“TaskProperties” = “task1Properties”
In “MethodInvoking” type “createTask1Invoke” and press ENTER. – You’ll be directed to the code editor.
(The activity properties look like this after you finished: )
In the method we initialize the task1-Properties we created before. The “ContentTypeId” is taken from the “Schema.xml” file of our “Workflow 1 Tasks” list where we created the Content Type “Workflow 1 Task 1”.
After that your method looks like this:
20. Back in the workflow designer. We drag a “While” activity from the Toolbox pane into the workflow behind the “Create Task” activity.
Into the “While” activity we drag a “Sequence” property from the Toolbox pane. We get:
Into the “Sequence” activity we drag a “OnTaskChanged” activity from the Toolbox pane. We get:
Now we edit the properties of the created “OnTaskChanged” activity. We have to set the “CorrelationToken” as described above. Additionally we specify new property bindings:
“TaskId” => Bind to the existing member “task1Guid”!!!
In the “Invoked” property add the method name “task1Changed1Invoke”. – The code editor will be opened.
(The activity properties look like this after you finished: )
21. In the code we add a property at class level:
This property we set inside the “OnWorkflowItemChanged” activity. It will contain the information whether the Workflow Task Item we created before was “Completed” by the assigned user.
In the “task1Changed1Invoke” method we set the “isFinished” property.
22. Now we need to specify the condition for the “While” activity. In the workflow designer select this activity. In the “Condition” property select “Code Condition” and expand the property. Enter “while1Invoke” in the subproperty with (the same) name “Condition”.
In the code editor enter the code for “while1Invoke” like this:
The “e.Result” property has to be “TRUE” as long as the while loop should run. It should not run anymore (“e.Result = false”) if “isFinished” is TRUE.
23. In the workflow designer add a “CompleteTask” activity behind the “While” activity. Drag the “CompleteTask” activity from the Toolbar pane into the workflow designer. We get:
Select the “completeTask1” activity and edit it’s properties in the Property pane.
Bind “TaskId” to the existing member “task1Guid”
Create a new member namend “task1Outcome” for binding to “TaskOutcome”
Set the “CorrelationToken” to “task1”
24. Whew!
25. Deploy the project. – It deployed successfully ?! – Congratulations!
26. Open the browser and navigate to “http://sharepoint.local/sites/workflow”. Open the “Workflow 1 Host List”. Add a new item… – The “Workflow 1” should start automatically.
(After F5 / page refresh:)
Click “In Process”. We see the Workflow Status page:
In the “Tasks” section open the context menu of the element “(no title)”. – We did not set a title for the created “Task 1”. This could be done in the method “createTask1Invoke”.
Click “Edit Item”.
There it is!!! :
Just edit a property and click “Save As Draft”.
On the Workflow Status page you see that the workflow is still running but was modified (“Last run”).
Then edit the task again. Click “Complete Task”.
After that the workflow is “Completed”:
27. We have done it! – Now we could add a “Task 2” for this “Workflow 1” where some other data is requested from users.
With that method you will be able to add custom forms for Workflow Tasks. You could also use forms you create with Silverlight !!!
I took me hours and hours to get to User Profile Service Application working.
These are the steps I’ve done in some scenarios.
Especial to migrate an SharePoint 2007 profile database to SharePoint 2010.
0. The databases for the User Profile Service Application must run on the standard instance of SQL Server. NEVER use a named instance. – Always use SQL Aliases instead!!!
1. (Skip this next step if you don’t need to migrate.) Backup the Shared Service Provider Database of your MOSS farm.
2. Restore the farm to your destination SQL Server instance and with the destination database name, e.g. “SP_SvcApp_UserProfile_Profiles”.
(In the MOSS farm the database was named “MOSS_SSP_Config” !!! –> It’s the configuration database of the Shared Service Provider.)
3. Create the User Profile Service Application with this PowerShell script:
During the creation of the User PRofile Service Application the MOSS database will be migrated!!
If you did no restore previously the profile database will be created in this step.
4. The farm account must be local admin on the farm server.
5. Go into the SQL Server Management Studio.
6. Set the farm account – in my example “sp_farm” – as SYSADMIN in your SQL Server instance.
7. Edit the User Logins for the (migrated) profile database.
The farm account should exist as login. – If not: add the account as “db_owner”.
Important: Set the schema for the farm account to “dbo”!
8. In the Windows Services Manager (Server Manager): restart a services that has the farm account as identity. OR restart the server!
9. Now: Log on locally with the farm account!! – Yes: logon with the farm account!
10. As farm account: go into the Central Administration –> Manage Services on Server –> Start the User Profile Synchronization service!
11. WAIT!
12. Press F5 on the “Managa Services on Server” page. Maybe wait some more . Have a look into the the SharePoint log. You should find lots of message. Search for “ILM” (using ULSViewer of course).
13. Sometimes – if you are a lucky person – the “User Profile Synchronization” service is startet. – Maybe not. In this case: Start over! – Last time it took me three times to get it working!
14. At the end: remove the farm account from the SYSADMIN group!
[Update / 01/26/2011]
15. Don’t forget to reset the Security Settings (“Administrators”) for the User Profile Service Application after you recreated them. – Go into the Central Administration -> Manage Service Applications -> select your User Profile Service Application -> click “Administrators” in the Ribbon. – You may find this empty!!! This is not correct. Insert here at least the farm account with “Full Control” permissions. And don’t forget to insert the search crawl account with “Retrieve People Data for Search Crawlers” permissions. Otherwise your People Search will not work!
Please give me feedback on this! – The configuration of the User Profile Services Application is a incredible mess.
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:
I tried to enable the service with the first Cmdlet. – This was the resulting error:
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 )
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-ne8) {
Write-Error"Please use a x64 PowerShell host!"return
}
#endregion#region Load SharePoint SnapIn and DLL
Remove-PSSnapinMicrosoft.SharePoint.PowerShell-ErrorActionSilentlyContinueAdd-PSSnapinMicrosoft.SharePoint.PowerShell-ErrorActionSilentlyContinue#Check available SharePoint Cmdletsif( (Get-Command-Noun SPWeb*) -eq$null ) {
Write-Error"SharePoint SnapIn not loaded. SharePoint cmdlets missing!"return
}
#endregioncls$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-ObjectMicrosoft.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-ErrorActionSilentlyContinue) -ne$null ) {
Write-Host"Successfull :-)"-ForegroundColorGreen
} else {
Write-Host"Failed :-("-ForegroundColorRed
}
After that, the “Enable-SPSessionStateService” works:
(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!!!