M2RootNamespace.Utils.ConsoleUtils.WithO365Context C# (CSharp) Method

WithO365Context() public method

public WithO365Context ( string siteUrl, string userName, string userPassword, Action action ) : void
siteUrl string
userName string
userPassword string
action Action
return void
        public virtual void WithO365Context(string siteUrl,
            string userName, string userPassword,
            Action<ClientContext> action)
        {
            // just a little check on URL, saves some typos
            new Uri(siteUrl);

            using (var context = new ClientContext(siteUrl))
            {
                context.Credentials = new SharePointOnlineCredentials(userName, GetSecurePasswordString(userPassword));

                action(context);
            }
        }

Usage Example

        static void Main(string[] args)
        {
            var siteUrl = "http://portal";
            var o365UserName = "******";
            var o365UserPassword = "******";

            var consoleUtils = new ConsoleUtils();

            consoleUtils.WithO365Context(siteUrl, o365UserName, o365UserPassword, context =>
            {
                // replace it with your M2 models
                var siteModel = default(ModelNode);
                var rotWebModel = default(ModelNode);

                // create a provision service - CSOMProvisionService or StandardCSOMProvisionService
                var provisionService = new CSOMProvisionService();

                // little nice thing, tracing the progress
                consoleUtils.TraceDeploymentProgress(provisionService);

                // deploy!
                provisionService.DeploySiteModel(context, siteModel);
                provisionService.DeployWebModel(context, rotWebModel);
            });
        }