Retrieve Extendedproperties Via SQL
Posted by Anders Vindberg
in Lunarmedia Blog
on the 19 Jan. 2009 (24,476 views).
In Community Server, dynamic user fields are saved in extended properties. In order to retrieve them in Sql add the following function:
/*------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
// Copyright (c) Telligent Systems Corporation. All rights reserved.
// </copyright>
------------------------------------------------------------------------------*/
CREATE function [dbo].[FetchExtendendAttributeValue] (
@Key nvarchar(4000),
@Keys nvarchar(4000),
@Values nvarchar(4000)
)
/*
CS uses ExtendedAttributes to allow metadata about a post, user, or section to be stored
in a special ':' delimited format. This enables storing metadata without adding new columns
to any tables, changing sprocs, etc.
However, any data stored in this format is not easily queryable (so use it wisely :)
Occassionaly, you may want to query against this data. This function should make that task simple!
Keys are stored in this format: string:S:Int:Int = Key + :S : Starting Location : Length :
An example: 'Theme:S:0:7:dummyTotalPosts:S:7:1:BannedUntil:S:8:21:UserBanReason:S:29:5:'
Values are stored in a single string with no spaces between them.
An example: 'default04/20/2005 12:16:41 AMOther'
Theme starts a 0 and continues for 7 characters (default)
dummyTotalPosts starts at 7 and coninutes for 1 character (0)
BannedUntil starts at 8 and continues for 21 characters (4/20/2005 12:16:41 AM)
*/
RETURNS nvarchar(4000)
AS
BEGIN
DECLARE @Value nvarchar(4000)
Declare @CharIndex int
Declare @StartIndex int
Declare @Len int
--Find the index of the key
Set @CharIndex = CHARINDEX(@Key + ':s',@Keys)
--If the key does not exist, return NULL
if(@CharIndex = 0)
RETURN NULL
--If the key is not the first, remove any leading keys
if(@CharIndex > 1)
Begin
Set @Keys = Stuff(@Keys,1,@CharIndex-1,'')
End
--Remove the Key from the keys list. This will
Set @Keys = Stuff(@Keys,1,Len(@Key+':S:'),'')
--Find the location of the : after the starting location
Set @CharIndex = CHARINDEX(':',@Keys)
--Grab the starting location
Set @StartIndex = SUBSTRING(@Keys,1,@CharIndex-1)
--Remove the starting location from the Keys
Set @Keys = Stuff(@Keys,1,@CharIndex,'')
--Find the lenght value's index
Set @CharIndex = CHARINDEX(':',@Keys)
--Find the lenghth value
Set @Len = SUBSTRING(@Keys,1,@CharIndex-1)
--Get the value from the values string
Set @Value = SUBSTRING(@Values,@StartIndex+1,@Len)
RETURN @Value
END
Source: http://code.communityserver.org...