Example #1
0
        protected virtual void EnsureMetapackLibrary()
        {
            NuGetLogUtils.Verbose("Detecting MetaPack library on target site...");

            var web  = _context.Web;
            var list = WebExtensions.LoadListByUrl(web, MetaPackConsts.SharePointLibraryUrl);

            if (list == null)
            {
                NuGetLogUtils.Verbose(string.Format("MetaPack library does not exist. Creating..."));

                var newList = web.Lists.Add(new ListCreationInformation
                {
                    TemplateType = (int)ListTemplateType.DocumentLibrary,
                    Title        = MetaPackConsts.SharePointLibraryTitle,
                    Url          = MetaPackConsts.SharePointLibraryUrl,
                    Description  = "MetaPack Gallery library. Stores NuGet packages with SharePoint solutions."
                });

                newList.Hidden        = true;
                newList.OnQuickLaunch = false;
                newList.NoCrawl       = true;

                newList.Update();
                _context.ExecuteQuery();

                // ensure schema

                NuGetLogUtils.Info("MetaPack library created");
                EnsureLibrarySchema(web);
            }
            else
            {
                NuGetLogUtils.Verbose("MetaPack library exists");
                EnsureLibrarySchema(web);
            }
        }
Example #2
0
        private void AddFileToSharePoint(Folder rootFolder, string filePath, Stream fileStream)
        {
            NuGetLogUtils.Verbose(string.Format("Saved file [{0}] to SharePoint", filePath));

            MemoryStream clonedNuPkgStream    = null;
            MemoryStream clonedZipNuPkgStream = null;

            try
            {
                if (filePath.ToLower().EndsWith(".nupkg"))
                {
                    clonedNuPkgStream    = new MemoryStream();
                    clonedZipNuPkgStream = new MemoryStream();

                    CopyStream(fileStream, clonedNuPkgStream);
                    fileStream.Position = 0;

                    CopyStream(fileStream, clonedZipNuPkgStream);
                    fileStream.Position = 0;

                    clonedNuPkgStream.Position    = 0;
                    clonedZipNuPkgStream.Position = 0;
                }
                else
                {
                    return;
                }

                var context = rootFolder.Context;

                var fileName       = Path.GetFileName(filePath);
                var fileFolderPath = Path.GetDirectoryName(filePath);

                InvalidatePathToFilesCache(fileFolderPath);

                var fileFolder = EnsureFolder(context, rootFolder, fileFolderPath);

                var file = fileFolder.Files.Add(new FileCreationInformation
                {
                    ContentStream = fileStream,
                    Overwrite     = true,
                    Url           = fileName
                });

                context.ExecuteQuery();

                if (clonedNuPkgStream != null)
                {
                    NuGetLogUtils.Verbose("Saving .nupkg metadata to SharePoint...");

                    var zipPackage = new ZipPackage(clonedNuPkgStream);
                    var zipArchive = new System.IO.Compression.ZipArchive(clonedZipNuPkgStream);

                    NuGetLogUtils.Info("Fetching nuspec file...");

                    var manifestFileName = string.Format("{0}.nuspec", zipPackage.Id);
                    var manifestFile     = zipArchive.GetEntry(manifestFileName);

                    var nuspecXmlValue = string.Empty;

                    using (var stramReader = new StreamReader(manifestFile.Open()))
                    {
                        nuspecXmlValue = stramReader.ReadToEnd();
                    }

                    NuGetLogUtils.Verbose("Updating nuspec and version values...");
                    if (!string.IsNullOrEmpty(nuspecXmlValue))
                    {
                        // update properties
                        var fileItem = file.ListItemAllFields;

                        context.Load(fileItem);

                        fileItem.ParseAndSetFieldValue("PackageNuspecXml", nuspecXmlValue);
                        fileItem.ParseAndSetFieldValue("PackageVersion", zipPackage.Version.ToString());
                        fileItem.ParseAndSetFieldValue("PackageId", zipPackage.Id);

                        fileItem.Update();
                        context.ExecuteQuery();
                    }
                }

                NuGetLogUtils.Verbose(string.Format("Saved file [{0}] to SharePoint", filePath));
            }
            finally
            {
                if (clonedNuPkgStream != null)
                {
                    clonedNuPkgStream.Dispose();
                    clonedNuPkgStream = null;
                }

                if (clonedZipNuPkgStream != null)
                {
                    clonedZipNuPkgStream.Dispose();
                    clonedZipNuPkgStream = null;
                }
            }
        }