Getting Profile Properties from ASP.Net and Community Server

by Brian Brewder October 05, 2008 17:40

If you are interested in getting the extended attributes from Community Server or ASP.Net membership, I've created a SQL Server database function that might help. I got the idea from another database function that's floating around the Internet (sorry, I don't know who to credit), but I had a little difficulty getting that one to work (I had the wrong version of another dependent function) so I rewrote it without the dependency.

Here's an example of using it (the code for the function is below the example). This example will return a list of all the users in Community Server along with some profile information.

SELECT  u.UserID, 
u.UserName,  
dbo.fn_GetProfileElement('MyCsProperty', up.PropertyNames, up.PropertyValues) AS MyCsProperty,
dbo.fn_GetProfileElement('MyAspProperty', p.PropertyNames, p.PropertyValuesString) AS MyAspProperty
FROM cs_users u
LEFT JOIN cs_UserProfile up ON (up.UserID = u.UserID)
LEFT JOIN aspnet_Profile p ON (p.UserID = u.MembershipID)


CREATE FUNCTION [dbo].[fn_GetProfileElement]
(
-- Add the parameters for the function here
@name NVARCHAR(100),
@keys NVARCHAR(4000),
@values NVARCHAR(4000)
)
RETURNS NVARCHAR(4000)
AS
BEGIN
-- If input is invalid, return null.
IF  @name IS NULL
OR LEN(@name) = 0
OR @keys IS NULL
OR LEN(@keys) = 0
OR @values IS NULL
OR LEN(@values) = 0
RETURN NULL
-- locate FieldName in Fields
DECLARE @pos AS INTEGER
DECLARE @endPos As INTEGER
DECLARE @valueStart AS INTEGER
DECLARE @valueLength AS INTEGER
-- Find the starting position of the key.
SET @pos = CHARINDEX(@name + ':S:', @keys, 0)
IF @pos = 0 RETURN NULL
-- Find the starting position of the value.
SET @pos = @pos + LEN(@name) + LEN(':S:')
SET @endPos = CHARINDEX(':', @keys, @pos)
SET @valueStart = CAST(SUBSTRING(@keys, @pos, @endPos - @pos) AS INT)
-- Find the length of the value.
SET @pos = @endPos + LEN(':')
SET @endPos = CHARINDEX(':', @keys, @pos)
SET @valueLength = CAST(SUBSTRING(@keys, @pos, @endPos - @pos) AS INT)
RETURN SUBSTRING(@values, @valueStart + 1, @valueLength)
END

Powered by BlogEngine.NET 1.6.0.0