Friday, November 29, 2013

SharePoint 2013 Maintenance Notification

SharePoint 2013 contains a SPMaintenaceWindow class which can use to give notifications to end users scoped to SharePoint content database. This is a simple PowerShell script you need to execute.

Ex:-

Enable Notification

$maintenanceStartDate  = "11/29/2013 08:00:00 AM" # Date when the maintenance will start
$maintenanceEndDate    = "11/30/2013 02:00:00 PM" # Date when the maintenance will stop
$notificationStartDate = "11/28/2013 06:00:00 AM" # Date when the message will start being displayed
$notificationEndDate   = "11/30/2013 02:00:00 PM" # Date when the message will stop being displayed
$maintenanceLink       = ""                      # This link will only appear if the maintenance duration is defined.
$maintenanceType       = "MaintenancePlanned"    # OPTIONS ARE: MaintenancePlanned | MaintenanceWarning
$readOnlyDays          = 1   # duration days
$readOnlyHours         = 6   # duration hours. 
$readOnlyMinutes       = 0   # duration minutes only appears if days and minutes are both zero
 
$maintenanceWindow = New-Object Microsoft.SharePoint.Administration.SPMaintenanceWindow
$maintenanceWindow.MaintenanceEndDate    = $maintenanceEndDate
$maintenanceWindow.MaintenanceStartDate  = $maintenanceStartDate
$maintenanceWindow.NotificationEndDate   = $notificationEndDate
$maintenanceWindow.NotificationStartDate = $notificationStartDate
$maintenanceWindow.MaintenanceType       = $maintenanceType
$maintenanceWindow.Duration              = New-Object System.TimeSpan( $readOnlyDays, $readOnlyHours, $readOnlyMinutes, 0)
$maintenanceWindow.MaintenanceLink       = $maintenanceLink
 
Get-SPContentDatabase | % {
    $_.MaintenanceWindows.Clear()
    $_.MaintenanceWindows.Add($maintenanceWindow)
    $_.Update()
}

 


Disable Notification



Get-SPContentDatabase | % {
    $_.MaintenanceWindows.Clear()
    $_.MaintenanceWindows.Add($maintenanceWindow)
    $_.Update()
}

Reference: http://blogs.msdn.com/b/josrod/archive/2013/09/30/sharepoint-2013-maintenance-window-notifications.aspx

Tuesday, November 5, 2013

How to Check User permission for the web, list or SharePoint Item in SharePoint JavaScript Object Model

Normally we need to perform tasks such as

  • Does current user is admin on the site
  • Does current user has list edit permissions
  • etc

SharePoint provides a method called doesUserHavePermissions to perform that. First of all we need to know how SharePoint defines User roles by assigning permission levels  such as Full Control, Contributor , design and etc.

For an example site admin is assigned by Full Control which is a composite of few permission items we called as permission kind.

Full Control - http://office.microsoft.com/en-001/windows-sharepoint-services-help/permission-levels-and-permissions-HA010100149.aspx

You can get all permission kind by http://msdn.microsoft.com/en-us/library/ee556747(v=office.14).aspx

Example One

Assume that we want to check whether current user is a admin of the site. For that we need to check user has  manageWeb permission kind. (actually we need to check other permission kinds assign to full control as well but if user has manage web it is more likely user can perform admin tasks, in my other example i will show how to check multiple permission kinds)

var ctx = new SP.ClientContext.get_current();
var web = context.get_web();
 
var ob = new SP.BasePermissions();
ob.set(SP.PermissionKind.manageWeb)
 
var per = web.doesUserHavePermissions(ob)
ctx.executeQueryAsync(
     function(){ 
          alert(per.get_value()); // If this is true user has permision if not no 
        },
     function(a,b){
         alert ("Something wrong");
 }
);
 

Example Two – Check multiple permission kinds


In here I'm going to check manageweb and managePermissions.



var ctx = new SP.ClientContext.get_current();
var web = context.get_web();
 
var ob = new SP.BasePermissions();
ob.set(SP.PermissionKind.manageWeb)
ob.set(SP.PermissionKind.managePermissions)
 
var per = web.doesUserHavePermissions(ob)
ctx.executeQueryAsync(
     function(){ 
          alert(per.get_value()); // If this is true user has permision if not no 
        },
     function(a,b){
         alert ("Something wrong");
 }
);

You can find REST interface here. http://msdn.microsoft.com/EN-US/library/office/jj245877.aspx