Example #1
0
        protected override PSDriveInfo NewDrive(PSDriveInfo drive)
        {
            WriteVerbose($"SPOProvider::NewDrive (Drive.Name = ’{drive.Name}’, Drive.Root = ’{drive.Root}’)");

            var spoParametes = DynamicParameters as SPODriveParameters;
            Web web          = null;

            if (spoParametes != null && spoParametes.Context != null)
            {
                var webUrl = spoParametes.Url ?? spoParametes.Context.Url;
                web = spoParametes.Context.Clone(webUrl).Web;
            }
            else if (spoParametes != null && spoParametes.Web != null)
            {
                var webUrl = spoParametes.Url ?? spoParametes.Web.EnsureProperty(w => w.Url);
                web = spoParametes.Web.Context.Clone(webUrl).Web;
            }
            else if (SPOnlineConnection.CurrentConnection != null)
            {
                var webUrl = (spoParametes != null && spoParametes.Url != null) ? spoParametes.Url : SPOnlineConnection.CurrentConnection.Context.Web.EnsureProperty(w => w.Url);
                web = SPOnlineConnection.CurrentConnection.Context.Clone(webUrl).Web;
            }
            else
            {
                WriteErrorInternal(PnPResources.NoConnection, drive.Root, ErrorCategory.ConnectionError);
            }

            if (web != null)
            {
#if DEBUG
                SetCounter(web.Context);
#endif
                var itemTimeout   = (spoParametes != null && spoParametes.ItemCacheTimeout != default(int)) ? spoParametes.ItemCacheTimeout : DefaultItemCacheTimeout;
                var webTimeout    = (spoParametes != null && spoParametes.WebCacheTimeout != default(int)) ? spoParametes.WebCacheTimeout : DefaultWebCacheTimeout;
                var normalizePath = NormalizePath(IsPropertyAvailable(web, "ServerRelativeUrl") ? web.ServerRelativeUrl : web.EnsureProperty(w => w.ServerRelativeUrl));

                //Set root to host root and current location to normalized path
                var normalizedDrive = new SPODriveInfo(new PSDriveInfo(drive.Name, drive.Provider, "/", drive.Description, drive.Credential, drive.DisplayRoot))
                {
                    Web             = web,
                    ItemTimeout     = itemTimeout,
                    WebTimeout      = webTimeout,
                    CurrentLocation = normalizePath
                };

                //Add web to cache
                normalizedDrive.CachedWebs.Add(new SPODriveCacheWeb
                {
                    Web         = normalizedDrive.Web,
                    LastRefresh = DateTime.Now,
                    Path        = normalizePath
                });

                return(normalizedDrive);
            }
            return(null);
        }
        protected override PSDriveInfo NewDrive(PSDriveInfo drive)
        {
            WriteVerbose(string.Format("SPOProvider::NewDrive (Drive.Name = ’{0}’, Drive.Root = ’{1}’)", drive.Name, drive.Root));

            var spoParametes = DynamicParameters as SPODriveParameters;
            Web web = null;

            if (spoParametes != null && spoParametes.Context != null)
            {
                var webUrl = spoParametes.Url ?? spoParametes.Context.Url;
                web = spoParametes.Context.Clone(webUrl).Web;
            }
            else if (spoParametes != null && spoParametes.Web != null)
            {
                var webUrl = spoParametes.Url ?? spoParametes.Web.EnsureProperty(w => w.Url);
                web = spoParametes.Web.Context.Clone(webUrl).Web;
            }
            else if (SPOnlineConnection.CurrentConnection != null)
            {
                var webUrl = (spoParametes != null && spoParametes.Url != null) ? spoParametes.Url : SPOnlineConnection.CurrentConnection.Context.Web.EnsureProperty(w => w.Url);
                web = SPOnlineConnection.CurrentConnection.Context.Clone(webUrl).Web;
            }
            else
            {
                WriteErrorInternal(PnPResources.NoConnection, drive.Root, ErrorCategory.ConnectionError);
            }

            if (web != null)
            {
            #if DEBUG
                SetCounter(web.Context);
            #endif
                var itemTimeout = (spoParametes != null && spoParametes.ItemCacheTimeout != default(int)) ? spoParametes.ItemCacheTimeout : DefaultItemCacheTimeout;
                var webTimeout = (spoParametes != null && spoParametes.WebCacheTimeout != default(int)) ? spoParametes.WebCacheTimeout : DefaultWebCacheTimeout;
                var normalizePath = NormalizePath(IsPropertyAvailable(web, "ServerRelativeUrl") ? web.ServerRelativeUrl : web.EnsureProperty(w => w.ServerRelativeUrl));

                //Set root to host root and current location to normalized path
                var normalizedDrive = new SPODriveInfo(new PSDriveInfo(drive.Name, drive.Provider, "/", drive.Description, drive.Credential, drive.DisplayRoot))
                {
                    Web = web,
                    ItemTimeout = itemTimeout,
                    WebTimeout = webTimeout,
                    CurrentLocation = normalizePath
                };

                //Add web to cache
                normalizedDrive.CachedWebs.Add(new SPODriveCacheWeb
                {
                    Web = normalizedDrive.Web,
                    LastRefresh = DateTime.Now,
                    Path = normalizePath

                });

                return normalizedDrive;
            }
            return null;
        }