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
So the problem is cut down to:
- in event handlers
- web.Site.SystemAccount.ID == properties.CurrentUserId
- in web pages or controls
- SPContext.Current.Web.CurrentUser.ID == SPContext.Current.Site.SystemAccount.ID
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
}
{
#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;
}
public bool isGroupMember(SPItemEventProperties properties)
{
bool isMember = false;
SPWeb web = properties.Web;
isMember = web.IsCurrentUserMemberOfGroup(web.Groups["MySite Owners"].ID);
web.Close();
return isMember;
}
Subscribe to:
Comments (Atom)