Partager via


WSRPConsumerWebPart does not have unique ID for userContextKey

A customer came up with an issue. WSRPConsumerWebPart’s userContextKey is always sending user’s display name when communicating with the producer. They were having a requirement that the userContextKey have to be a unique value to identify the user.

Upon a little digging found that when you are using the WSRPConsumerWebPart, the userContextKey have 2 options to pass to the WSRP Provider Service.

  • Anonymous
  • Current User Name

When option 2 is used, it always passes the Display Name of the user as in the SharePoint user profile (SPUser.Name). The SharePoint user profile is usually populated from the AD fields. The Display Name is not guaranteed to be Unique for the list of users unlike SPUser.LoginName which is unique in AD.

 

Why does this happen?

When using "Current User Name" option is selected in the WSRPConsumerWebPart, SharePoint hard codes it to SPWeb.CurrentUser.Name property which effectively uses SPUser.Name property, which in turn returns the user's display name and NOT the actual user ID or samAccount information of the user.

The DisplayName can be duplicate in the the AD and is not guaranteed to be unique.

As this is the behavior of WSRPConsumerWebPart and SPUser.Name is hard coded in to the WSRPConsumerWebPart to be returned for userContextKey, only the workarounds can be applied.

 

Workaround 1

You can update the display name field in the AD properties of the user to something unique and re-import the profiles into SharePoint
 

Workaround 2

Use the code to update the SPUser properties in SharePoint. You can get more details on the SPUser.Name property at https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spuser.name.aspx. This article also contains an example of how to modify/update SPUser object.
 

Workaround 3

You can edit user profile property (Name) in the SSP (Central Administration –> Shared Services Administration –> User Profile and Properties –> View Profile Properties –> Edit User Profile Property (Name) )

By default "Name” property is mapped to displayName property of AD.

You can map it to an AD property which is Unique in the AD like mail which contains the Email address of the user. Import the profiles again and your Name, property will be a unique field and can be used in WSRPConsumerWebPart’s userContextKey.

 

Hope this helps someone out banging head for unique value of userContextKey.

 

Happy Coding

Comments

  • Anonymous
    May 21, 2009
    PingBack from http://microsoft-sharepoint.simplynetdev.com/wsrpconsumerwebpart-does-not-have-unique-id-for-usercontextkey/

  • Anonymous
    December 15, 2009
    I love you. If i could find you i would kiss you.

  • Anonymous
    December 15, 2009
    The client I have does not like the work-arounds and desires for me to modify the WebPart so that it uses my own class... public class WSRPConsumerWebPartExt : WSRPConsumerWebPart That users userContextKey instead of display name. Do you have any idea what function i could overwrite to do this? I'm not sure where to even start. -Nicholas

  • Anonymous
    December 15, 2009
    Hi Nicholas, You cannot just override the webpart and get it working. Basically the userContextKey is used from WSRPMarkupProxy.aspx page sitting in your "/_layouts" folder of SharePoint and WSRP consumer webpart uses this page to fetch the data. Instead of overriding your class from OOTB WSRP consumer webpart, writing a WSRP consumer from scratch would be a lot easier thing to do. You can actually go through this article in Feb 2006 issue of MSDN magazine (http://msdn.microsoft.com/en-us/magazine/cc163651.aspx) which talks about creating a WSRP consumer for WinForms in C#. There is also a code for WinForm app to get you started. -Manpreet