Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Thursday, February 16, 2012

Retrieving Users from Subsite SharePoint Groups.


try
            {
                string strAllUsers = string.Empty;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    strAllUsers += "<Table width='100%' cellpadding='0' cellspacing='0'>";
                    strAllUsers += "<Tr>";
                    strAllUsers += "<Td colspan='4' bgcolor='#add8e6'>";
                    strAllUsers += "</Td>";
                    strAllUsers += "</Tr>";
                    strAllUsers += "<Tr>";
                    strAllUsers += "<Td colspan='4' Align='Center'>";
                    strAllUsers += "<Strong>";
                    strAllUsers += "Subsites Users using this site";
                    strAllUsers += "</Strong>";
                    strAllUsers += "</Td>";
                    strAllUsers += "</Tr>";
                    strAllUsers += "<Tr>";
                    strAllUsers += "<Td bgcolor='#add8e6'>";
                    strAllUsers += "<Strong>";
                    strAllUsers += "UserName";
                    strAllUsers += "</Strong>";
                    strAllUsers += "</Td>";
                    strAllUsers += "<Td bgcolor='#add8e6' nowrap='nowrap'>";
                    strAllUsers += "<Strong>";
                    strAllUsers += "SharePoint Group";
                    strAllUsers += "</Strong>";
                    strAllUsers += "</Td>";
                    strAllUsers += "<Td bgcolor='#add8e6'>";
                    strAllUsers += "<Strong>";
                    strAllUsers += "EmailID";
                    strAllUsers += "</Strong>";
                    strAllUsers += "</Td>";
                    strAllUsers += "<Td bgcolor='#add8e6' nowrap='nowrap'>";
                    strAllUsers += "<Strong>";
                    strAllUsers += "SubWeb Name";
                    strAllUsers += "</Strong>";
                    strAllUsers += "</Td>";
                    strAllUsers += "</Tr>";
                    strAllUsers += "<Tr>";
                    strAllUsers += "<Td colspan='4' bgcolor='#add8e6'>";
                    strAllUsers += "</Td>";
                    strAllUsers += "</Tr>";
                    SPWeb currentWeb = SPContext.Current.Web;
                    foreach (SPWeb objWeb in currentWeb.GetSubwebsForCurrentUser())
                    {
                        foreach (SPGroup grp in objWeb.Groups)
                        {
                            foreach (SPUser user in grp.Users)
                            {
                                string strUser = string.Empty;
                                string strGroup = string.Empty;
                                string strUserEmail = string.Empty;
                                string strSubWebTitle = string.Empty;
                                string strSubWebUrl = string.Empty;
                                strUser = user.Name;
                                strGroup = grp.Name;
                                strUserEmail = user.Email;
                                strSubWebTitle = objWeb.Title;
                                strSubWebUrl = objWeb.Url;
                                strAllUsers += "<Tr>";
                                strAllUsers += "<Td>";
                                strAllUsers += strUser;
                                strAllUsers += "</Td>";
                                strAllUsers += "<Td>";
                                strAllUsers += strGroup;
                                strAllUsers += "</Td>";
                                strAllUsers += "<Td>";
                                strAllUsers += strUserEmail;
                                strAllUsers += "</Td>";
                                strAllUsers += "<Td>";
                                strAllUsers += "<a href='"+strSubWebUrl+"'>"+strSubWebTitle+"</a>";
                                strAllUsers += "</Td>";
                                strAllUsers += "</Tr>";
                                strAllUsers += "<Tr>";
                                strAllUsers += "<Td colspan='4' bgcolor='#add8e6'>";
                                strAllUsers += "</Td>";
                                strAllUsers += "</Tr>";
                            }
                        }
                    }
                    strAllUsers += "</Table>";
                    writer.Write(strAllUsers);
                });
            }
            catch (Exception ex)
            {
                writer.Write(ex.ToString());
            }

No comments:

Post a Comment