Thursday, March 29, 2012

XSLS to check for custom properties

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <xmp><xsl:copy-of select="*"/></xmp> </xsl:template> </xsl:stylesheet>

Monday, March 19, 2012

How to check if current user operates as System Account

While trying to find out a better way to check if current user operates as System Account, other then checking if the login name is “SHAREPOINT/system” string, I’ve stumbled upon SPSite.SystemAccount property.
So the problem is cut down to:
  • in event handlers
    1. web.Site.SystemAccount.ID == properties.CurrentUserId
  • in web pages or controls
    1. SPContext.Current.Web.CurrentUser.ID == SPContext.Current.Site.SystemAccount.ID
You can change SPContext.Current.Web with your SPWeb instance variable. Same goes for SPContext.Current.Site.

Monday, March 5, 2012

remove all permissions for a list item in sharepoint

 private static void RemoveAllPermissions(SPItemEventProperties properties)
        {
            #region NewCode
            SPSite siteColl = new SPSite(properties.Web.Url);
            SPWeb site = siteColl.OpenWeb();
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID))
                {
                    using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(site.ID))
                    {
                        ElevatedSite.AllowUnsafeUpdates = true;
                        SPList omylist = ElevatedSite.Lists[properties.ListTitle];
                        //SPListItem myitem = ElevatedSite.GetListItem(properties.ListItem.Url);
                        SPListItem myitem = omylist.Items[properties.ListItem.UniqueId];
                        //The below function Breaks the role assignment inheritance for the list and gives the current list its own copy of the role assignments
                        myitem.BreakRoleInheritance(true);
                        //Get the list of Role Assignments to list item and remove one by one.
                        SPRoleAssignmentCollection SPRoleAssColn = myitem.RoleAssignments;
                        for (int i = SPRoleAssColn.Count - 1; i >= 0; i--)
                        {
                            SPRoleAssColn.Remove(i);
                        }
                    }
                }
            });
            #endregion

        }

Check if an User is a member of certain group

Use the following method which says whether an user is a member of sharepoint group

public bool isGroupMember(SPItemEventProperties properties)
        {
            bool isMember = false;
            SPWeb web = properties.Web;
            isMember = web.IsCurrentUserMemberOfGroup(web.Groups["MySite Owners"].ID);
            web.Close();
            return isMember;
        }