Quantcast
Channel: Sip Stories » Lync 2010
Viewing all articles
Browse latest Browse all 6

How to: Query the Lync database for a user’s presence state

$
0
0

It always bothered me that the dbanalyze user report didn’t return the user’s current presence state. Seemed like such a simple and useful thing – anyways it finally bothered me enough to write a sql query to do it.

A note on what I’m doing here – I’m querying the rtcdyn backend database’s PublishedInstance table and grabbing the image field Data. Trick to being able to read the XML in the Data field – convert it to varbinary, then to varchar.

I’m looking for the aggregate 1 container (containernumber=2) which feeds what the “Federated” “Colleagues” and “Family” permissions group contacts would see as your state.  If you’re interested in all the container groups and what they’re for they’re documented quite nicely on MSDN
here and here

After that I’m digging through the XML for lines that have ‘aggregateState’, then pulling the tag which is stored as an integer (values documented here). 

Lastly – to catch users that aren’t registered at all, I’m unioning what we queried with the rtc Resource table then cheating a little and using MIN to pick the right value (“Offline-Not Registerd Here” is the higest value of the possibilities – I had a “God please forgive me for this line of code” moment when I wrote it, but it faded quickly)

Just replace ‘sipusername%’ with any valid SQL filter to find the aggregate state of that sip user.

Enjoy

declare @sipQuery nvarchar(250)
set @sipQuery = 'sipusername%'

select Publisher, MIN(Status) as Status
from
(select Publisher, Status=
CASE
when Availability BETWEEN 0 AND 2999 then 'Not defined:'+Availability
when Availability BETWEEN 3000 AND 4499 then 'Available'
when Availability BETWEEN 4500 and 5999 then 'Available - Idle'
when Availability BETWEEN 6000 and 7499 then 'Busy'
when Availability BETWEEN 7500 and 8999 then 'Busy - Idle'
when Availability BETWEEN 9000 and 11999 then 'Do not Disturb'
when Availability BETWEEN 12000 and 14999 then 'Be right back'
when Availability BETWEEN 15000 and 17999 then 'Away'
when Availability > 18000 then 'Offline'
end
from
(
select Publisher,
substring(
substring(PublicationDocument,patIndex('%<availability>%',PublicationDocument)+14,50),0,
patIndex('%<availability>%',substring(PublicationDocument,patIndex('%<availability>%',PublicationDocument)+14,50))
) Availability
from
(select UserAtHost Publisher,ContainerNum,CONVERT(varchar(4000),convert(varbinary(4000),Data)) PublicationDocument
from rtcdyn.dbo.PublishedInstance tblPublishedInstance,
rtc.dbo.Resource tblResource
where tblPublishedInstance.PublisherId = tblResource.ResourceId) as PublishedDocuments
where LEN(replace(PublicationDocument,'aggregateState','')) < LEN(PublicationDocument)
and ContainerNum = 2
) as PublisherAndAvailability
where Publisher like @sipQuery
union
select UserAtHost Publisher, 'Offline-Not Registered Here' Status
from rtc.dbo.Resource
where UserAtHost like @sipQuery) as PublisherAndStatus
group by Publisher


Viewing all articles
Browse latest Browse all 6

Trending Articles