Archive for October, 2013

SharePoint User\Group columns and People Picker Controls

So recently I had to perform some custom code that would add an entry from a people picker control to a user\group column and back again (on another form).  Going from the people picker control to SharePoint is really easy (the code snippet’s below are obviously vb.net.  I am trying to keep most of my examples on this site in C#, but this particular client I wrote this for was a VB shop at the time).


Protected Sub btnSave_Onclick(sender As Object, e As System.EventArgs) Handles btnSave.Click
'I use a dictionary here as I find it a very efficient method for holding all
'my data columns and their values (I have a lot more than shown here)
Dim dataToInsert As Dictionary(Of String, String) = New Dictionary(Of String, String)
Dim spControl As SharePointControl = New SharePointControl 'SharePointControl is a custom class for all my SP data access

If (ppManager.ResolvedEntities.Count > 0) Then
Dim manager As PickerEntity = TryCast(ppManager.ResolvedEntities(0), PickerEntity)
dataToInsert.Add(AttendanceSupportStrings.ManagerColumn, manager.Key)
Else
lblMessage.Text = "Manager field cannot be blank"
Exit Sub
End If
'...
'do other validation and value storage here
'...
Try
spControl.AddListItem(SPContext.Current.Site, dataToInsert)
Catch ex as exception
lblMessage.Text = (String.Format("An error occurred adding this entry.  Error returned: {0}", ex.Message))
lblMessage.Visible = True
End Try

 

Next, cast the value to an SPUser type and update the list. Please note: I realize some of you may be wondering why I am not going directly from the people picker to SPUser.  The reason being is that in most cases you are updating more than one field at a time and I wanted to show you a method (at least the one I use) to store columns of multiple types

'Use the dictionary key as the column name and the value as the column value
Sub AddListItem(sPSite As SPSite, dataToInsert As Dictionary(Of String, String))

Try
Using Web As SPWeb = sPSite.OpenWeb
Dim splist As SPList = Web.Lists(AttendanceSupportStrings.AttendanceListName)
Dim listItem As SPListItem = splist.AddItem()

For Each dictEntry As KeyValuePair(Of String, String) In dataToInsert
Select Case (dictEntry.Key)
Case AttendanceSupportStrings.ManagerColumn
listItem(dictEntry.Key) = Me.GetEmployeeByID(sPSite, dictEntry.Value)
'...
'More conditional updates here
'...
End Select
Next
listItem.Update()
End Using
Catch......

Private Function GetEmployeeByID(sPSite As SPSite, userName As String) As SPUser

Dim serviceContext As SPServiceContext
serviceContext = SPServiceContext.GetContext(sPSite)
Dim profileManager As New UserProfileManager(serviceContext)

If (profileManager.UserExists(userName)) Then
Dim spUser As SPUser = sPSite.OpenWeb.EnsureUser(userName)
Return spUser
Else
Throw New UserNotFoundException("This user is not found in SharePoint.  For the workflow process to proceed ensure user has valid email")
End If

End Function

 

That may seem like a lot of code just to upload, but remember it was written to handle more than just the one column. It also illustrates a method to upload an entire list row’s worth of data.

Now to go from a User Column to a people picker is quite a bit less code because I am only doing it one column at a time and access the column directly.  I am not building a dictionary to hold different field types nor updating a number of fields at once.  However, I do think that going from a User column to a People Picker is a bit more convoluted.  You actually first cast the user column into a SPUser object, then you put the user object’s login name into a Picker Entity.  You then add the picker entity to an ArrayList and finally you then add the array list to the picker object.

If (Not spListItem(AttendanceSupportStrings.UnionRepColumn) Is Nothing) Then
Dim unionContact As PickerEntity = New PickerEntity()
Dim unionList As ArrayList = New ArrayList()
Dim userUnion As SPUser = New SPFieldUserValue(spListItem.Web, spListItem(AttendanceSupportStrings.UnionRepColumn).ToString).User

unionContact.Key = userUnion.LoginName
unionList.Add(unionContact)
ppHRCContact.UpdateEntities(unionList)

End If

 

So hopefully this illustrates a method that can be used to take a value from a People Picker in a custom form up to SharePoint and then back again.

Creating a complete installation of SharePoint 2010 without needing a domain

SharePoint 2007 allowed for a complete farm installation of SharePoint without the need of a domain.  It would have been nice to carry this on with SharePoint 2010, but unfortunately Microsoft decided this wasn’t important and instead provided a method to perform a “stand-alone” install.  The stand-alone install is great for business users and demonstrators who are trying to show off products to prospective clients as a fairly high-end laptop is all you need (thus you don’t really want a domain as it will be used in a mobile setting).  This solution does not work well for developers and\or other users that would like to use a full SQL 2008 install instead of SQL Express that is used in a stand-alone install.

Enter PowerShell.  With PowerShell you can configure SharePoint to install a full version, connect to a full version of SQL 2008 and create content DBs all without the use of domain accounts.  This is extremely useful for developers who travel with laptops and don’t want to have to worry about domains.  Be warned though, some products like PowerPivot do require domain accounts (no workaround that I am aware of) and if needed pretty much make what I am about to show you pretty useless.

There are some preparation steps you need to do first (you don’t have to do this, but I found it saved some headaches later).

  1. On your PC\Laptop\VM create an account that you wish to use to connect to the SQL DB.  Basically make this your farm account.
  2. Make this account an administrator on the server you are installing SQL and SP on.

Setup instructions:

  1. Create a SharePoint Farm account (ex. SP_Farm).  This account does not require administration rights to the system.
  2. Logged on with an administrator account, install SharePoint Server.
  3. When prompted for type of installation, select Server Farmimage
  4. When the installation is complete remove the check mark for: Run the SharePoint Products and Technologies Configuration Wizard now.
  5. Next log out of the system.  And login with the administrator password you created in the preparation steps.
  6. Click on Start -> All Programs -> Microsoft SharePoint 2010 Products.  Right click on SharePoint 2010 Management Shell and select Run as administrator.
  7. This is where PowerShell allows you to setup SharePoint without a domain account.  Type in the following command within the PowerShell command window you opened up:

[code language=”powershell”]<br /><br />New-SPConfigurationDatabase -DatabaseServer "ComputerName" -DatabaseName "instance\DBname" -FarmCredentials "computername\farm account" -Passphrase "passphrase used to connect SP servers to the farm"<br /><br />[/code]

Once that command is complete you can now complete the SharePoint 2010 Products Configuration Wizard.