Thursday, June 14, 2012

Get All Users from a User Collection



SPFieldUserValueCollection afterAttendees=new SPFieldUserValueCollection(web, properties.AfterProperties["Attendees"].ToString());

to get a single user use foreach with SPFieldUserValue

Check if Current User have Specific Permissions


SPRoleDefinition roleDefinitionRead = spWeb.RoleDefinitions.GetByType(SPRoleType.Reader);
spList.DoesUserHavePermissions(user, roleDefinitionRead.BasePermissions);

Wednesday, June 13, 2012

Some useful power shell commands


Copy DLL's From GAC

C:\Windows\assembly\GAC_MSIL\cd dllname
C:\Windows\assembly\GAC_MSIL\dllname\cd dllvirsion_dllpublickytoken(simply press tab after typing cd to get version and public key token)
C:\Windows\assembly\GAC_MSIL\dllname\dllvirsion_dllpublickytoken\copy dllname path_to_save


Set UIVersion for a site

$web = Get-SPWeb http://suinshr00002:666
  $web.UIVersion=3
  $web.Update()

set UI Version  for all sites of a site collection

$w = get-spweb http://sharepoint/sites/team/subsite
$w.webs | ForEach-Object {$_.UIversion = 4; $_.UIVersionConfigurationEnabled = $false; $_.update()} 

Tuesday, June 12, 2012

Disable Event Handlers when Updating a list Programatically

HandleEventFiring eventFiring = new HandleEventFiring();
eventFiring.AccDisableEventFiring();
listItem.Update();
eventFiring.AccEnableEventFiring();


-------------------

public class HandleEventFiring : SPItemEventReceiver
        {
            public void AccDisableEventFiring()
            {
                this.DisableEventFiring();
            }

            public void AccEnableEventFiring()
            {
                this.EnableEventFiring();
            }
        }

Friday, June 8, 2012

Calculate Business days - SharePoint List calculated column


The following calculated column formula calculates business excluding weekends, based on start date and end date columns in a list.

=IF(AND((WEEKDAY(To,2))<(WEEKDAY(From,2)),((WEEKDAY(From,2))-(WEEKDAY(To,2)))>1),(((DATEDIF(From,To,"D")+1))-(FLOOR((DATEDIF(From,To,"D")+1)/7,1)*2)-2),(((DATEDIF(From,To,"D")+1))-(FLOOR((DATEDIF(From,To,"D")+1)/7,1)*2)))

From is the start date column name here and To is the end date.

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

Wednesday, February 29, 2012

Logging errors/exceptions of sharepoint into Logs

NameSpace: using Microsoft.SharePoint.Administration;

SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("My Custom category"TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message + ex.StackTrace, null);