Search This Blog

Total Pageviews

Saturday, November 26, 2011

SharePoint Enable RMS Using Object Model

Step 1 Enable IRM on the farm

SPWebService svc = SPFarm.Local.Services.GetValue();
SPIrmSettings irmSettings = svc.IrmSettings;
irmSettings.IrmRMSEnabled = true;
//set true or false based on the situation
irmSettings.IrmRMSUseAD = true;
irmSettings.IrmRMSCertServer = "certificate server here";
irmSettings.IrmChanges = irmSettings.IrmChanges + 1;
svc.Update();



Step2 Set the IRM properties for a document library
SPSite site = new SPSite("http:////");
SPList spList = site.OpenWeb().Lists["list_name"];
SPFolder folder = spList.RootFolder;
spList.IrmEnabled = true; //corresponds to "Restrict permission to documents in this library on download"
// BELOW SET=2, RESET=0
folder.Properties["vti_irm_IrmPrint" ] = 2; // Allow users to print documents
folder.Properties["vti_irm_IrmVBA" ] = 2; // Allow users to access content programmatically
folder.Properties["vti_irm_IrmOffline" ] = 2; // Users must verify their credentials every nOfflineDays
spList.IrmReject = true; // do not allow users to upload documents that do not support IRM
spList.IrmExpire = true; //sto[ restricting permissions to documents in this library on expiry date
folder.Properties["vti_irm_IrmOfflineDays"] = nOfflineDays;
// integer representing number of days after which user needs to verify credentials
folder.Properties["vti_irm_IrmExpireDate" ] = dtExpire.ToString("r", CultureInfo.InvariantCulture);
// date on which to stop restricting IRM permissions to documents
folder.Properties["vti_irm_IrmTitle" ] = "Permission Policy Title";
folder.Properties["vti_irm_IrmDescription"] = "Permission Policy Description";
folder.Update();
spList.Update();

Tuesday, November 22, 2011

Add an existing site collection Group to sub site with modified Roles

OBJECTIVE

The site collection contains a group named “All Members” with “Contributor” privilege.
I want to add this group programmatically to a sub site and modify its roles from “Contributor” to “Read” without modifying the original group.

The following are the variables used in the code:
       -          site: the SPSite object of the site collection being used
-          web: the SPWeb object of the sub site you want to add the group to.

SOLUTION:
SPGroup group = site.RootWeb.SiteGroups["All Members"];
SPRoleAssignment roleAssignment = new SPRoleAssignment(group);
SPRoleDefinition roleDefinition = site.RootWeb.RoleDefinitions["Read"];
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
if (!web.HasUniqueRoleAssignments)
     web.BreakRoleInheritance(true);
web.RoleAssignments.Add(roleAssignment);

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

Working with Group: quick review:
      1)       BREAK INHERITANCE  
         2)       ADD NEW GROUP TO SITE COLLECTION 
         3)       ASSOCIATE THE NEW GROUP TO A WEB 
         4)       ASSIGNMENT OF THE ROLES
         5)       ADD USERS TO THE GROUP 
         6)       REMOVE ALL ROLES
     

1) BREAK INHERITANCE
if (!web.HasUniqueRoleAssignments)
            web.BreakRoleInheritance(true);

2) ADD NEW GROUP TO SITE COLLECTION:
web.SiteGroups.Add("MyNewGroup", web.AssociatedOwnerGroup, null"MyNewGroup description");

3) ASSOCIATE THE NEW GROUP TO A WEB:
web.AssociatedGroups.Add(group);
web.Update();

4) ASSIGNMENT OF THE ROLES:
SPGroup group = web.SiteGroups["MyNewGroup"];
SPRoleAssignment roleAssignment = new SPRoleAssignment(group);
SPRoleDefinition roleDefinition = site.RootWeb.RoleDefinitions["Read"];
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
web.RoleAssignments.Add(roleAssignment);

5) ADD USERS TO THE GROUP:
SPUser user = web.EnsureUser("domain\MyUser"); 
SPGroup group = web.Groups["MyNewGroup"];                        
if(user!=null && group!=null)
     group.AddUser(user);

6) REMOVE ALL ROLES:
SPRoleAssignmentCollection SPRoleAssColn = web.RoleAssignments;
for (int i = SPRoleAssColn.Count - 1; i >= 0; i--)
{
      SPRoleAssColn.Remove(i);
}     

Thursday, November 10, 2011

Start User Profile Synchronization Service through Powershell cmd


Function ConvertTo-UnsecureString

([System.Security.SecureString]$string)
   {
   $unmanagedString =

[System.Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUni

code($string)
    $unsecureString =

[System.Runtime.InteropServices.Marshal]::PtrToStringUni

($unmanagedString)
     

[System.Runtime.InteropServices.Marshal]::ZeroFreeGlobalAllocUnicode

($unmanagedString)
   
        return $unsecureString
     }


$syncMachine = Get-SPServer "sp2010dev"
$profApp = Get-SPServiceApplication | where {$_.Name -eq "User Profile

Service Application 1"}
$account = Get-Credential "localdev\spfarm"
if ($syncMachine.Address -eq $env:ComputerName) {
    $syncSvc = Get-SPServiceInstance -Server $env:ComputerName | where

{$_.TypeName -eq "User Profile Synchronization Service"}
    $syncSvc.Status =

[Microsoft.SharePoint.Administration.SPObjectStatus]::Provisioning
    $syncSvc.IsProvisioned = $false
    $syncSvc.UserProfileApplicationGuid = $profApp.Id
    $syncSvc.Update()
    $profApp.SetSynchronizationMachine($syncMachine.Address,

$syncSvc.Id, $account.UserName, (ConvertTo-UnsecureString

$account.Password))
}

if ($syncSvc.Status -ne "Online") {
    Write-Host "Starting User Profile Synchronization Service..."
    Start-SPServiceInstance $syncSvc
}
do {Start-Sleep 2} while ((Get-SPServiceInstance -Server

$env:ComputerName | where {$_.TypeName -eq "User Profile

Synchronization Service"}).Status -ne "Online")

Start User Profile Synchronization Service through Powershell cmd


Function ConvertTo-UnsecureString

([System.Security.SecureString]$string)
   {
   $unmanagedString =

[System.Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUni

code($string)
    $unsecureString =

[System.Runtime.InteropServices.Marshal]::PtrToStringUni

($unmanagedString)
     

[System.Runtime.InteropServices.Marshal]::ZeroFreeGlobalAllocUnicode

($unmanagedString)
   
        return $unsecureString
     }


$syncMachine = Get-SPServer "sp2010dev"
$profApp = Get-SPServiceApplication | where {$_.Name -eq "User Profile

Service Application 1"}
$account = Get-Credential "localdev\spfarm"
if ($syncMachine.Address -eq $env:ComputerName) {
    $syncSvc = Get-SPServiceInstance -Server $env:ComputerName | where

{$_.TypeName -eq "User Profile Synchronization Service"}
    $syncSvc.Status =

[Microsoft.SharePoint.Administration.SPObjectStatus]::Provisioning
    $syncSvc.IsProvisioned = $false
    $syncSvc.UserProfileApplicationGuid = $profApp.Id
    $syncSvc.Update()
    $profApp.SetSynchronizationMachine($syncMachine.Address,

$syncSvc.Id, $account.UserName, (ConvertTo-UnsecureString

$account.Password))
}

if ($syncSvc.Status -ne "Online") {
    Write-Host "Starting User Profile Synchronization Service..."
    Start-SPServiceInstance $syncSvc
}
do {Start-Sleep 2} while ((Get-SPServiceInstance -Server

$env:ComputerName | where {$_.TypeName -eq "User Profile

Synchronization Service"}).Status -ne "Online")

Tuesday, November 1, 2011

Get All SubWebs using Client Object Model


  static string mainpath = "http://triad102:1001";
        static void Main(string[] args)
        {

            getSubWebs(mainpath);
            Console.Read();
        }

        public static  void  getSubWebs(string path)
        {
         
            try
            {
                ClientContext clientContext = new ClientContext( path );
                Web oWebsite = clientContext.Web;
                clientContext.Load(oWebsite, website => website.Webs, website => website.Title);
                clientContext.ExecuteQuery();
                foreach (Web orWebsite in oWebsite.Webs)
                {
                    string newpath = mainpath + orWebsite.ServerRelativeUrl;
                    getSubWebs(newpath);
                    Console.WriteLine(newpath + "\n" + orWebsite.Title );
                }
            }
            catch (Exception ex)
            {
               
            }
         
         
        }