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.


 <asp:TemplateField HeaderText="User Name">
                            <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>

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;
                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;

Thursday, November 21, 2013

Farm PropertyBag in SharePoint

Creating and Configuring the Farm level property enable the Farm administrator to control the configuration settings on Site / Web level. He can expose the some of the general values such as variable,connection strings and etc..to all the server available in Farm. To use SPFarm class include the "SharePoint.Administration" namespace in your code. SPFarm class will not work unless you connected to at least one Physical server. In the VM which is cloned or mimic from the actual server,you cannot execute this class based code in your solution. Secondly,your current application pool account and Farm account must be same otherwise you'll get the "Access Denied or Unauthorized exception even though you used "RunWithElevated Privilege" delegation. You can identify what account being used by your Farm,by navigating to your "Central Administration Page",->Security header under,Click on "Configure Service Account". Select the "Farm account from the drop down list->It will shows the account which is used by your Farm.
 protected void Page_Load(object sender, EventArgs e)
            SPSecurity.CodeToRunElevated code = new SPSecurity.CodeToRunElevated(UpdateMyKey);
        public void UpdateMyKey()
            SPWeb web = SPContext.Current.Web;
            web.AllowUnsafeUpdates = true;
            SPFarm farm = SPFarm.Local;
            farm.Properties.Add("MyKey1", "MyValue");
            web.AllowUnsafeUpdates = false;

//Retrieve the Farm Value in your Site/Sub Site Level.
        public void GetValue(Object sender,EventArgs e)
            SPFarm farm = SPFarm.Local;
            String propertyValue = farm.Properties["MyKey1"].ToString();


Friday, November 15, 2013

Reading File contents in SharePoint programmatically

Line of code to perform reading the XML contents from the SharePoint file and converting the XML String to DataSet.
                   SPSite site = null;
                   SPWeb web = null;
                    site = new SPSite((SPContext.Current.Site.Url));
                   web = site.OpenWeb();
                   var file = web.GetFile("Shared%20Documents/temp.xml");
                  string fileUrl = SPContext.Current.Web.Url + "/Shared%20Documents/temp.xml";
                  byte[] binFile = file.OpenBinary();
                  Stream stream = new MemoryStream(binFile);
                  DataSet ds = new DataSet();
                   spItems.DataSource = ds.Tables[0];