Tuesday, November 26, 2013

Lync presence indicator in SharePoint webpart

Recently I have searched alot almost nearly 3 hours to work around to "Show Lync presence" in my SharePoint webPart or Application page.

Out of this search,quickly I come up with simple yet more robust way of binding the Lync indicator.

There will not be mess javascript or wiring up the server object model code the html markup.

Simply I retrieved the collection of SPUser object and bind them to entity class and then to Gridview.

Snippet


 <asp:TemplateField HeaderText="User Name">
                        <ItemTemplate>
                            <img alt="presence status" border="0" height="15" width="15" src="/_layouts/images/imnhdr.gif" onload="IMNRC('<%#Eval("EmailId") %>')" ShowOfflinePawn="1" id="<%#Eval("SysId") %>"  />
                            <asp:Label runat="server" id="lableUser" Text='<%#Eval("UserName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>


Code behind to retrieve the SPUser from the SPGroup


   private List<Users> GetAllUsersFromGroup()
        {
            SPSite site = null;
            SPWeb web = null;
            var list = new List<Users>();
            Users objName = null;
            try
            {
 
 
                site = new SPSite(SPContext.Current.Site.Url);
                string w = SPContext.Current.Web.ServerRelativeUrl;
                web = site.AllWebs[w.ToString()];
                list.AddRange(from SPGroup @group in web.Groups
                              let currentUserRole = web.RoleAssignments.GetAssignmentByPrincipal(@group)
                              from SPRoleDefinition role in currentUserRole.RoleDefinitionBindings
                              from SPUser user in @group.Users
                              select new Users
                              {
                                  UserName = user.Name,
                                  UserLogin = user.LoginName,
                                  PermissionType = role.Name,
                                  SysId = user.Sid,
                                  EmailId = user.Email,
                                  GroupName = currentUserRole.Member.Name,
 
                              });
 
            }
            catch (Exception exception)
            {
                throw new SPException(exception.Message.ToString());
            }
            return list;
        }