Monday, 18 June 2012

Site Helper

 STEP 1: Create a class file like SiteHelper.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web;
using System.Xml;
using System.Data;

namespace VCSB.INTRANET.BLL
{
    public class SiteHelper
    {
        public static string SiteUrl
        {
            get { return SPContext.Current.Web.Url; }
        }
       
        public static string CurrentUserRoleType()
        {
            string Curntroletype = string.Empty;
            using (SPSite site = new SPSite(SPContext.Current.Web.Url))
            {
                site.AllowUnsafeUpdates = true;

                using (SPWeb spweb = site.OpenWeb())
                {
                    spweb.AllowUnsafeUpdates = true;
                    SPGroupCollection groupCollection = spweb.Groups;
                    foreach (SPGroup grp in spweb.CurrentUser.Groups)
                    {
                        foreach (SPGroup group in groupCollection)
                        {
                            if (grp.Name.Equals(group.Name))
                            {

                                SPContext.Current.Site.CatchAccessDeniedException = false;

                                // Retrieve all user roles assigned for the current user for the current web.
                                SPRoleDefinitionBindingCollection userRoles =SPContext.Current.Web.AllRolesForCurrentUser;
                                string xml = userRoles.Xml;
                                XmlDocument doc = new XmlDocument();
                                doc.LoadXml(xml);
                                foreach (XmlNode item in doc.DocumentElement.ChildNodes)
                                {
                                    //Check If Role Type == Reader - Do something
                                    if (item.Attributes["Type"].Value.Equals(SPRoleType.Reader.ToString()))
                                    {
                                        Curntroletype= SPRoleType.Reader.ToString();
                                    }
                                    //Administrators access - Full Control - Do something
                                    else if (item.Attributes["Type"].Value.Equals(SPRoleType.Administrator.ToString()))
                                    {
                                        Curntroletype= SPRoleType.Administrator.ToString();
                                    }
                                    //Contributor access - Contribute - Do something
                                    else if (item.Attributes["Type"].Value.Equals(SPRoleType.Contributor.ToString()))
                                    {
                                       Curntroletype= SPRoleType.Contributor.ToString();
                                    }
                                    //Web Designer access - Design rights- Do something
                                    else if (item.Attributes["Type"].Value.Equals(SPRoleType.WebDesigner.ToString()))
                                    {
                                        Curntroletype= SPRoleType.WebDesigner.ToString();
                                    }
                                    //Limited access - Do something
                                    else if (item.Attributes["Type"].Value.Equals(SPRoleType.Guest.ToString()))
                                    {
                                       Curntroletype= SPRoleType.Guest.ToString();
                                    }
                                    //No access on Current Web- Do something
                                    else if (item.Attributes["Type"].Value.Equals(SPRoleType.None.ToString()))
                                    {
                                        Curntroletype = SPRoleType.Guest.ToString();
                                    }
                                    else
                                    {
                                        Curntroletype = SPRoleType.None.ToString();
                                    }
                                    break;
                                }

                            }
                            //Get Role Assignments for Current User - If he has been directly assigned permissions
                            try { }
                            catch (Exception) {/*Best attempt to catch Exceptions*/}

                            finally
                            {
                                SPContext.Current.Site.CatchAccessDeniedException = true;

                            }
                        }
                    }
                    spweb.AllowUnsafeUpdates = false;
                } site.AllowUnsafeUpdates = false;
            } return Curntroletype;
           
        }

        #region GetCurrentUserGroup
        public static string GetCurrentUserGroup()
        {
            string GroupName = "";
         
            using (SPSite site = new SPSite(SiteUrl))
            {
                site.AllowUnsafeUpdates = true;

                using (SPWeb spweb = site.OpenWeb())
                {
                    spweb.AllowUnsafeUpdates = true;
                    SPGroupCollection groupCollection = spweb.Groups;
                    foreach (SPGroup grp in spweb.CurrentUser.Groups)
                    {
                        foreach (SPGroup group in groupCollection)
                        {
                            if (grp.Name.Equals(group.Name))
                            {                               
                                GroupName = grp.Name; break;
                            }
                            //Get Role Assignments for Current User - If he has been directly assigned permissions
                            try { }
                            catch (Exception) {/*Best attempt to catch Exceptions*/}

                        }
                    }
                    spweb.AllowUnsafeUpdates = false;
                } site.AllowUnsafeUpdates = false;
            }
            return GroupName;
        }

        #endregion

        #region VCSBSiteAdmin
        public static bool IsSiteAdmin()
        {
            SPContext currentContext;
            try
            {
                //Getting the current context         
                currentContext = SPContext.Current;
            }
            catch (InvalidOperationException)
            {
                currentContext = null;
            }
            if (currentContext != null && currentContext.Web.CurrentUser != null)
            {
                if (SPContext.Current.Web.CurrentUser.IsSiteAdmin)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region CurrentUserLoginName
        public static string CurrentUserLoginName()
        {
            string userName = "NA";
            SPContext currentContext;
            try
            {
                //Getting the current context         
                currentContext = SPContext.Current;
            }
            catch (InvalidOperationException)
            {
                currentContext = null;
            }
            if (currentContext != null && currentContext.Web.CurrentUser != null)
            {
                SPSite spSite;
                SPWeb spWeb;
                using (spSite = new SPSite(SiteUrl))
                {
                    using (spWeb = spSite.OpenWeb())
                    {
                        userName = spWeb.CurrentUser.LoginName;
                    }
                }
            }
            return userName;
        }
      
        #endregion

        public static DataTable GetCurrentSiteGroup()
        {
            DataTable dtGroups = new DataTable();
            dtGroups.Columns.Add("GroupName");
            dtGroups.Columns.Add("Url");
          

            using (SPSite site = new SPSite(SiteUrl))
            {
                site.AllowUnsafeUpdates = true;

                using (SPWeb spweb = site.OpenWeb())
                {
                    spweb.AllowUnsafeUpdates = true;
                    SPGroupCollection groupCollection = spweb.Groups;
                    if (groupCollection.Count != 0 && groupCollection != null)
                    {
                        foreach (SPGroup group in groupCollection)
                        {
                            DataRow dr = dtGroups.NewRow();
                            dr["GroupName"] = group.Name;
                            dr["Url"] = group.ID;

                            dtGroups.Rows.Add(dr);

                        }
                    }
                    spweb.AllowUnsafeUpdates = false;
                } site.AllowUnsafeUpdates = false;
            }
            return dtGroups;
        }

        public static DataTable GetMyTeamSites()
        {
            DataTable dtDept = new DataTable();

            dtDept.Columns.Add("SiteName");
            dtDept.Columns.Add("SiteUrl");

            SPWebCollection UserDepartments = SPContext.Current.Site.RootWeb.GetSubwebsForCurrentUser();
            if (UserDepartments.Count != 0 && UserDepartments != null)
            {
                foreach (SPWeb item in UserDepartments)
                {
                    SPWebCollection MyTeamSites = SPContext.GetContext(item).Web.GetSubwebsForCurrentUser();
                    foreach (SPWeb Teamsite in MyTeamSites)
                    {
                        if (item.Name != "PressReleases" && item.Name != "Search")
                        {
                            DataRow dr = dtDept.NewRow();

                            dr["SiteName"] = Convert.ToString(Teamsite.Name);
                            dr["SiteUrl"] = Convert.ToString(Teamsite.Url);
                            dtDept.Rows.Add(dr);
                        }
                    }
                   
                }

            }
            return dtDept;
        }


        public static DataTable GetRecentlyViewedPages()
        {
            DataTable dt = SPContext.Current.Web.GetUsageData(Microsoft.SharePoint.Administration.SPUsageReportType.url, Microsoft.SharePoint.Administration.SPUsagePeriodType.lastMonth);
            dt.DefaultView.Sort = "Recent Month DESC";
            DataView dv = dt.DefaultView;
            dv.Sort = "Recent Month DESC";

            DataTable dtRecent = new DataTable();
            dtRecent.Columns.Add("Page");
            dtRecent.Columns.Add("Url");
            dtRecent.Columns.Add("Hits");
            dtRecent.Columns.Add("RecentDate");
            int i = 0;

            foreach (DataRow item in dv.ToTable().Rows)
            {
                if (i < 5 && Convert.ToString(item["Folder"]) == "pages")
                {
                    DataRow dr = dtRecent.NewRow();
                    dr["Page"] = Convert.ToString(item["Page"]);
                    dr["Url"] = Convert.ToString(SPContext.Current.Site.Url + "/" + item["Folder"] + "/" + item["Page"]);
                    dr["Hits"] = Convert.ToString(item["Recent Month"]);
                    dr["RecentDate"] = Convert.ToString(item["Most Recent Day"]);
                    dtRecent.Rows.Add(dr);
                    i++;
                }

            }
            return dtRecent;

        }
    }
}

No comments:

Post a Comment

SharePoint online - Get List-item attachments and Display to div

Step 1 : Create a List ex: TestList and attach few images Step 2 : Copy and Pastet the below coding in App.js var  Items =  null ; ...