Programmatically set Time Off for the System User in CRM 2011


We can make use of below sample code to set the Time Off for the user programmatically. In the below code we are setting time off of duration 8 hours on 7th May 2013.


IServiceManagement<IOrganizationService> orgServiceManagement =
 ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri("https://orgname.crm5.dynamics.com/XRMServices/2011/Organization.svc"));

AuthenticationCredentials authCredentials = new AuthenticationCredentials();
 authCredentials.ClientCredentials.UserName.UserName = "user@domain.onmicrosoft.com";
 authCredentials.ClientCredentials.UserName.Password = "password";
 AuthenticationCredentials tokenCredentials = orgServiceManagement.Authenticate(authCredentials);

OrganizationServiceProxy organizationProxy = new OrganizationServiceProxy(orgServiceManagement, tokenCredentials.SecurityTokenResponse);

 // Get the user id
 Guid userid = ((WhoAmIResponse)organizationProxy.Execute(new WhoAmIRequest())).UserId;

// Get the calendar id of the user
 Entity systemUserEntity = organizationProxy.Retrieve("systemuser", userid, new ColumnSet(new String[] { "calendarid"}));

// Retrieve the calendar of the user
 Entity userCalendarEntity = organizationProxy.Retrieve("calendar", ((Microsoft.Xrm.Sdk.EntityReference)(systemUserEntity.Attributes["calendarid"])).Id, new ColumnSet(true));

// Retrieve the calendar rules defined in the calendar
 EntityCollection calendarRules = (EntityCollection)userCalendarEntity.Attributes["calendarrules"];

// Create a new inner calendar
 Entity newInnerCalendar = new Entity("calendar");
 newInnerCalendar.Attributes["businessunitid"] = new EntityReference("businessunit", ((Microsoft.Xrm.Sdk.EntityReference)(userCalendarEntity["businessunitid"])).Id);
 Guid innerCalendarId = organizationProxy.Create(newInnerCalendar);

// Create a new calendar rule and assign the inner calendar id to it
 Entity calendarRule = new Entity("calendarrule");
 calendarRule.Attributes["description"] = "Time Off Rule";
 calendarRule.Attributes["duration"] = 480;
 calendarRule.Attributes["extentcode"] = 2;
 calendarRule.Attributes["pattern"] = "FREQ=DAILY;INTERVAL=1;COUNT=1";
 calendarRule.Attributes["rank"] = 0;
 calendarRule.Attributes["timezonecode"] = 190;
 calendarRule.Attributes["innercalendarid"] = new EntityReference("calendar", innerCalendarId);

// starting at 12:00 on 7 May
 calendarRule.Attributes["starttime"] = new DateTime(2013, 5, 7,0,0,0,DateTimeKind.Utc);
 calendarRules.Entities.Add(calendarRule);

// assign all the calendar rule back to the user calendar
 userCalendarEntity.Attributes["calendarrules"] = calendarRules;
 // update the user calendar entity that has the new rule
 organizationProxy.Update(userCalendarEntity);

Entity calendarRule1 = new Entity("calendarrule");

// duration of 8 hours
 calendarRule1.Attributes["duration"] = 480;
 calendarRule1.Attributes["effort"] = 2.0;
 calendarRule1.Attributes["issimple"] = true;


 calendarRule1.Attributes["offset"] = 0;
 calendarRule1.Attributes["rank"] = 0;
 // subcode 6= vacation
 calendarRule1.Attributes["subcode"] = 6;
 // time code 2 = unavailable
 calendarRule1.Attributes["timecode"] = 2;
 calendarRule1.Attributes["timezonecode"] = -1;
 calendarRule1.Attributes["calendarid"] = new EntityReference("calendar", innerCalendarId);

EntityCollection innerCalendarRules = new EntityCollection();
 innerCalendarRules.EntityName = "calendarrule";
 innerCalendarRules.Entities.Add(calendarRule1);

newInnerCalendar.Attributes["calendarrules"] = innerCalendarRules;
 newInnerCalendar.Attributes["calendarid"] = innerCalendarId;
 organizationProxy.Update(newInnerCalendar);

Hope it helps


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

Please share your thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog - PPUG.ORG

AI - Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.