FileIsNodeXLWorkbook
    (
        String filePath
    )
    {
        Debug.Assert( !String.IsNullOrEmpty(filePath) );

        if ( ServerDocument.IsCustomized(filePath) )
        {
            Guid oSolutionID = new Guid(ApplicationUtil.SolutionID);

            using ( ServerDocument oServerDocument =
                new ServerDocument(filePath) )
            {
                if (oServerDocument.SolutionId == oSolutionID)
                {
                    return (true);
                }
            }
        }

        return (false);
    }
Beispiel #2
0
        //</Snippet5>


        //<Snippet6>
        private void AddCustomizationUsingDocumentPath(string documentPath, string deployManifestPath)
        {
            int runtimeVersion = 0;

            try
            {
                runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);

                // Make sure that this document does not yet have any Visual Studio Tools
                // for Office customizations.
                if (runtimeVersion == 0)
                {
                    Uri deploymentManifestUri = new Uri(deployManifestPath);
                    ServerDocument.AddCustomization(documentPath, deploymentManifestUri);
                    MessageBox.Show("The document was successfully customized.");
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("The document is already customized.");
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
            }
            catch (UnknownCustomizationFileException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document has a file " +
                                                     "extension that is not supported by Visual Studio Tools for Office.");
            }
            catch (DocumentNotCustomizedException ex)
            {
                System.Windows.Forms.MessageBox.Show("The document could not be customized.\n" +
                                                     ex.Message);
            }
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            string assemblyLocation           = "";
            Guid   solutionID                 = new Guid();
            Uri    deploymentManifestLocation = null;
            string documentLocation           = "";

            string[] nonpublicCachedDataMembers = null;

            for (int i = 0; i <= args.Count() - 1; i++)
            {
                Console.WriteLine(args[i]);
                string[] oArugment = args[i].Split('=');

                switch (oArugment[0])
                {
                case "/assemblyLocation":
                    assemblyLocation = oArugment[1];
                    break;

                case "/deploymentManifestLocation":
                    if (!Uri.TryCreate(oArugment[1], UriKind.Absolute, out deploymentManifestLocation))
                    {
                        Console.WriteLine("Error creating URI");
                    }
                    break;

                case "/documentLocation":
                    documentLocation = oArugment[1];
                    break;

                case "/solutionID":
                    solutionID = Guid.Parse(oArugment[1]);
                    break;
                }
            }
            try
            {
                ServerDocument.RemoveCustomization(documentLocation);
                ServerDocument.AddCustomization(documentLocation, assemblyLocation,
                                                solutionID, deploymentManifestLocation,
                                                true, out nonpublicCachedDataMembers);
            }
            catch (System.IO.FileNotFoundException)
            {
                Console.WriteLine("The specified document does not exist.");
            }
            catch (System.IO.IOException)
            {
                Console.WriteLine("The specified document is read-only.");
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine("The customization could not be removed.\n" +
                                  ex.Message);
            }
            catch (DocumentNotCustomizedException ex)
            {
                Console.WriteLine("The document could not be customized.\n" +
                                  ex.Message);
            }
        }
    ConvertNodeXLWorkbook
    (
        String otherWorkbookFile,
        String convertedWorkbookFile
    )
    {
        Debug.Assert( !String.IsNullOrEmpty(otherWorkbookFile) );
        Debug.Assert( File.Exists(otherWorkbookFile) );
        Debug.Assert( !String.IsNullOrEmpty(convertedWorkbookFile) );

        // The application's template is needed to get the customization
        // information.

        String sTemplatePath;

        if ( !ApplicationUtil.TryGetTemplatePath(out sTemplatePath) )
        {
            throw new NodeXLWorkbookConversionException(
                ApplicationUtil.GetMissingTemplateMessage() );
        }

        try
        {
            File.Copy(otherWorkbookFile, convertedWorkbookFile, true);
        }
        catch (UnauthorizedAccessException)
        {
            throw new NodeXLWorkbookConversionException(
                "The converted copy already exists and is read-only.  It can't"
                + " be overwritten."
                );
        }
        catch (IOException oIOException)
        {
            if ( oIOException.Message.Contains(
                "it is being used by another process") )
            {
                throw new NodeXLWorkbookConversionException(
                    "The converted copy already exists and is open in Excel."
                    + "  It can't be overwritten."
                );
            }

            throw (oIOException);
        }

        // Remove the other customization.

        try
        {
            if (ServerDocument.GetCustomizationVersion(
                convertedWorkbookFile) > 0)
            {
                ServerDocument.RemoveCustomization(convertedWorkbookFile);
            }
        }
        catch (Microsoft.VisualStudio.Tools.Applications.Runtime.
            UnknownCustomizationFileException)
        {
            throw new NodeXLWorkbookConversionException(
                "The file doesn't appear to be an Excel workbook."
            );
        }

        // Create a ServerDocument from the application's template.  The
        // solution ID and deployment manifest name will be obtained from this.

        using ( ServerDocument oTemplateServerDocument =
            new ServerDocument(sTemplatePath, FileAccess.Read) )
        {
            // For some reason, ServerDocument.AddCustomization() also requires
            // a path to the NodeXL assembly file, even though it doesn't get
            // embedded in the document.

            String sAssemblyFile = new Uri(
                Assembly.GetExecutingAssembly().CodeBase).LocalPath;

            String [] asNonPublicCachedDataMembers;

            ServerDocument.AddCustomization(convertedWorkbookFile,
                sAssemblyFile, oTemplateServerDocument.SolutionId,
                oTemplateServerDocument.DeploymentManifestUrl, false,
                out asNonPublicCachedDataMembers);
        }
    }
        static private void InitializeCachedDataSet()
        {
            //<Snippet2>
            //<Snippet3>
            AdventureWorksDataSet.AdventureWorksLTDataSet productDataSet =
                new AdventureWorksDataSet.AdventureWorksLTDataSet();
            AdventureWorksDataSet.AdventureWorksLTDataSetTableAdapters.ProductTableAdapter productTableAdapter =
                new AdventureWorksDataSet.AdventureWorksLTDataSetTableAdapters.ProductTableAdapter();

            string workbookPath = System.Environment.GetFolderPath(
                Environment.SpecialFolder.MyDocuments) +
                                  @"\AdventureWorksReport\bin\Debug\AdventureWorksReport.xlsx";
            ServerDocument serverDocument1 = null;

            //</Snippet3>

            //<Snippet4>
            try
            {
                productTableAdapter.Fill(productDataSet.Product);
                Console.WriteLine("The local dataset is filled.");

                serverDocument1 = new ServerDocument(workbookPath);
                CachedDataHostItem dataHostItem1 =
                    serverDocument1.CachedData.HostItems["AdventureWorksReport.Sheet1"];
                CachedDataItem dataItem1 = dataHostItem1.CachedData["adventureWorksLTDataSet"];

                // Initialize the worksheet dataset with the local dataset.
                if (dataItem1 != null)
                {
                    dataItem1.SerializeDataInstance(productDataSet);
                    serverDocument1.Save();
                    Console.WriteLine("The data is saved to the data cache.");
                    Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("The data object is not found in the data cache.");
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (System.IO.FileNotFoundException)
            {
                Console.WriteLine("The specified workbook does not exist.");
            }
            finally
            {
                if (serverDocument1 != null)
                {
                    serverDocument1.Close();
                }

                Console.WriteLine("\n\nPress Enter to close the application.");
                Console.ReadLine();
            }
            //</Snippet4>
            //</Snippet2>
        }
        static private void ReadCachedDataSet()
        {
            //<Snippet9>
            //<Snippet10>
            AdventureWorksDataSet.AdventureWorksLTDataSet productDataSet =
                new AdventureWorksDataSet.AdventureWorksLTDataSet();
            string workbookPath = System.Environment.GetFolderPath(
                Environment.SpecialFolder.MyDocuments) +
                                  @"\AdventureWorksReport\bin\Debug\AdventureWorksReport.xlsx";
            ServerDocument serverDocument1 = null;

            //</Snippet10>

            //<Snippet11>
            try
            {
                serverDocument1 = new ServerDocument(workbookPath);
                CachedDataHostItem dataHostItem1 =
                    serverDocument1.CachedData.HostItems["AdventureWorksReport.Sheet1"];
                CachedDataItem dataItem1 = dataHostItem1.CachedData["adventureWorksLTDataSet"];

                if (dataItem1 != null)
                {
                    Console.WriteLine("Before reading data from the cache dataset, the local dataset has " +
                                      "{0} rows.", productDataSet.Product.Rows.Count.ToString());

                    // Read the cached data from the worksheet dataset into the local dataset.
                    System.IO.StringReader schemaReader = new System.IO.StringReader(dataItem1.Schema);
                    System.IO.StringReader xmlReader    = new System.IO.StringReader(dataItem1.Xml);
                    productDataSet.ReadXmlSchema(schemaReader);
                    productDataSet.ReadXml(xmlReader);

                    Console.WriteLine("After reading data from the cache dataset, the local dataset has " +
                                      "{0} rows.", productDataSet.Product.Rows.Count.ToString());
                }
                else
                {
                    Console.WriteLine("The data object is not found in the data cache.");
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                Console.WriteLine("The specified workbook does not exist.");
            }
            catch (System.Xml.XmlException)
            {
                Console.WriteLine("The data object has invalid XML information.");
            }
            finally
            {
                if (serverDocument1 != null)
                {
                    serverDocument1.Close();
                }

                Console.WriteLine("\n\nPress Enter to close the application.");
                Console.ReadLine();
            }
            //</Snippet11>
            //</Snippet9>
        }
        static private void ModifyCachedDataSet()
        {
            //<Snippet5>
            //<Snippet6>
            AdventureWorksDataSet.AdventureWorksLTDataSet productDataSet =
                new AdventureWorksDataSet.AdventureWorksLTDataSet();
            string workbookPath = System.Environment.GetFolderPath(
                Environment.SpecialFolder.MyDocuments) +
                                  @"\AdventureWorksReport\bin\Debug\AdventureWorksReport.xlsx";
            ServerDocument serverDocument1 = null;

            //</Snippet6>

            //<Snippet7>
            try
            {
                serverDocument1 = new ServerDocument(workbookPath);
                CachedDataHostItem dataHostItem1 =
                    serverDocument1.CachedData.HostItems["AdventureWorksReport.Sheet1"];
                CachedDataItem dataItem1 = dataHostItem1.CachedData["adventureWorksLTDataSet"];

                if (dataItem1 != null)
                {
                    Console.WriteLine("Before reading data from the cache dataset, the local dataset has " +
                                      "{0} rows.", productDataSet.Product.Rows.Count.ToString());

                    // Read the cached data from the worksheet dataset into the local dataset.
                    System.IO.StringReader schemaReader = new System.IO.StringReader(dataItem1.Schema);
                    System.IO.StringReader xmlReader    = new System.IO.StringReader(dataItem1.Xml);
                    productDataSet.ReadXmlSchema(schemaReader);
                    productDataSet.ReadXml(xmlReader);

                    Console.WriteLine("After reading data from the cache dataset, the local dataset has " +
                                      "{0} rows.", productDataSet.Product.Rows.Count.ToString());

                    // Modify the prices of each product in the local dataset.
                    foreach (AdventureWorksDataSet.AdventureWorksLTDataSet.ProductRow row in
                             productDataSet.Product.Rows)
                    {
                        if (row.ProductCategoryID < 20)
                        {
                            row.ListPrice = row.ListPrice + (row.ListPrice * (Decimal).10);
                        }
                        else
                        {
                            row.ListPrice = row.ListPrice - (row.ListPrice * (Decimal).10);
                        }
                    }

                    // Write the modified local dataset to the worksheet dataset using the DiffGram format.
                    System.Text.StringBuilder stringIn  = new System.Text.StringBuilder();
                    System.IO.StringWriter    stringOut = new System.IO.StringWriter(stringIn);
                    productDataSet.WriteXml(stringOut, System.Data.XmlWriteMode.DiffGram);
                    dataItem1.Xml = stringIn.ToString();

                    serverDocument1.Save();
                    Console.WriteLine("The product prices have been modified.");
                }
                else
                {
                    Console.WriteLine("The data object is not found in the data cache.");
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                Console.WriteLine("The specified workbook does not exist.");
            }
            catch (System.Xml.XmlException)
            {
                Console.WriteLine("The data object has invalid XML information.");
            }
            finally
            {
                if (serverDocument1 != null)
                {
                    serverDocument1.Close();
                }

                Console.WriteLine("\n\nPress Enter to close the application.");
                Console.ReadLine();
            }
            //</Snippet7>
            //</Snippet5>
        }
Beispiel #8
0
        public void Execute(AddInPostDeploymentActionArgs args)
        {
            XElement parameters = XElement.Parse(args.PostActionManifestXml);

            //configurabili
            string dataDirectory = @"Data\";
            string file          = parameters.Attribute("filename").Value;

            //statici
            string sourcePath            = args.AddInPath;
            string destPath              = Environment.ExpandEnvironmentVariables(Base.Simboli.LocalBasePath);
            Uri    deploymentManifestUri = args.ManifestLocation;
            string sourceFile            = Path.Combine(sourcePath, dataDirectory, file);
            string destFile              = Path.Combine(destPath, file);

            switch (args.InstallationStatus)
            {
            case AddInInstallationStatus.InitialInstall:
                if (!Directory.Exists(destPath))
                {
                    Directory.CreateDirectory(destPath);
                }

                System.IO.File.Copy(sourceFile, destFile, true);

                if (ServerDocument.IsCustomized(destFile))
                {
                    ServerDocument.RemoveCustomization(destFile);
                }

                ServerDocument.AddCustomization(destFile, deploymentManifestUri);

                break;

            case AddInInstallationStatus.Update:
                string dirUPDATE  = Path.Combine(destPath, "UPDATE");
                string fileUPDATE = Path.Combine(dirUPDATE, file);
                if (!Directory.Exists(dirUPDATE))
                {
                    Directory.CreateDirectory(dirUPDATE);
                }

                System.IO.File.Copy(sourceFile, fileUPDATE, true);

                if (ServerDocument.IsCustomized(fileUPDATE))
                {
                    ServerDocument.RemoveCustomization(fileUPDATE);
                }

                ServerDocument.AddCustomization(fileUPDATE, deploymentManifestUri);

                break;

            case AddInInstallationStatus.Uninstall:
                if (System.IO.File.Exists(destFile))
                {
                    //rimuovo file di installazione
                    System.IO.File.Delete(destFile);

                    //rimuovo directory di update
                    string update = Path.Combine(destPath, "UPDATE");
                    if (Directory.Exists(update) && !Directory.EnumerateFileSystemEntries(update).Any())
                    {
                        Directory.Delete(update);
                    }

                    //rimuovo directory PSO
                    if (!Directory.EnumerateFileSystemEntries(destPath).Any())
                    {
                        Directory.Delete(destPath);
                    }
                }
                break;
            }
        }
        ConvertNodeXLWorkbook
        (
            String otherWorkbookFile,
            String convertedWorkbookFile
        )
        {
            Debug.Assert(!String.IsNullOrEmpty(otherWorkbookFile));
            Debug.Assert(File.Exists(otherWorkbookFile));
            Debug.Assert(!String.IsNullOrEmpty(convertedWorkbookFile));

            // The application's template is needed to get the customization
            // information.

            String sTemplatePath;

            if (!ApplicationUtil.TryGetTemplatePath(out sTemplatePath))
            {
                throw new NodeXLWorkbookConversionException(
                          ApplicationUtil.GetMissingTemplateMessage());
            }

            try
            {
                File.Copy(otherWorkbookFile, convertedWorkbookFile, true);
            }
            catch (UnauthorizedAccessException)
            {
                throw new NodeXLWorkbookConversionException(
                          "The converted copy already exists and is read-only.  It can't"
                          + " be overwritten."
                          );
            }
            catch (IOException oIOException)
            {
                if (oIOException.Message.Contains(
                        "it is being used by another process"))
                {
                    throw new NodeXLWorkbookConversionException(
                              "The converted copy already exists and is open in Excel."
                              + "  It can't be overwritten."
                              );
                }

                throw (oIOException);
            }

            // Remove the other customization.

            try
            {
                if (ServerDocument.GetCustomizationVersion(
                        convertedWorkbookFile) > 0)
                {
                    ServerDocument.RemoveCustomization(convertedWorkbookFile);
                }
            }
            catch (Microsoft.VisualStudio.Tools.Applications.Runtime.
                   UnknownCustomizationFileException)
            {
                throw new NodeXLWorkbookConversionException(
                          "The file doesn't appear to be an Excel workbook."
                          );
            }

            // Create a ServerDocument from the application's template.  The
            // solution ID and deployment manifest name will be obtained from this.

            using (ServerDocument oTemplateServerDocument =
                       new ServerDocument(sTemplatePath, FileAccess.Read))
            {
                // For some reason, ServerDocument.AddCustomization() also requires
                // a path to the NodeXL assembly file, even though it doesn't get
                // embedded in the document.

                String sAssemblyFile = new Uri(
                    Assembly.GetExecutingAssembly().CodeBase).LocalPath;

                String [] asNonPublicCachedDataMembers;

                ServerDocument.AddCustomization(convertedWorkbookFile,
                                                sAssemblyFile, oTemplateServerDocument.SolutionId,
                                                oTemplateServerDocument.DeploymentManifestUrl, false,
                                                out asNonPublicCachedDataMembers);
            }
        }
Beispiel #10
0
        public override void Install(IDictionary stateSaver)
        {
            string[] nonpublicCachedDataMembers = null;


            // Use the following for debugging during the install
            //string parameters = "Parameters in Context.Paramters:";
            //foreach (DictionaryEntry parameter in Context.Parameters)
            //{
            //    parameters = parameters + "\n" + parameter.Key + ":" + parameter.Value;
            //}

            //MessageBox.Show(parameters);

            //MessageBox.Show("total items in parameters: " + Context.Parameters.Count);
            //MessageBox.Show("Document Manifest Location:" + Context.Parameters["deploymentManifestLocation"]);

            Uri deploymentManifestLocation = null;

            if (Uri.TryCreate(
                    Context.Parameters["deploymentManifestLocation"],
                    UriKind.RelativeOrAbsolute,
                    out deploymentManifestLocation) == false)
            {
                throw new InstallException(
                          "The location of the deployment manifest " +
                          "is missing or invalid.");
            }
            string documentLocation =
                Context.Parameters["documentLocation"];

            if (String.IsNullOrEmpty(documentLocation))
            {
                throw new InstallException(
                          "The location of the document is missing.");
            }
            string assemblyLocation =
                Context.Parameters["assemblyLocation"];

            if (String.IsNullOrEmpty(assemblyLocation))
            {
                throw new InstallException(
                          "The location of the assembly is missing.");
            }

            // use the following for debugging
            MessageBox.Show(documentLocation);

            if (ServerDocument.IsCustomized(documentLocation))
            {
                ServerDocument.RemoveCustomization(documentLocation);
            }
            ServerDocument.AddCustomization(
                documentLocation,
                assemblyLocation,
                SolutionID,
                deploymentManifestLocation,
                false,
                out nonpublicCachedDataMembers);
            stateSaver.Add("documentlocation", documentLocation);
            base.Install(stateSaver);
        }
Beispiel #11
0
        //</Snippet4>

        //<Snippet5>
        private void DisplayDataCacheContents(string documentPath)
        {
            int            runtimeVersion  = 0;
            ServerDocument serverDocument1 = null;

            try
            {
                runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);

                if (runtimeVersion != 3)
                {
                    MessageBox.Show("This document does not have a Visual Studio Tools for " +
                                    "Office customization, or it has a customization that was created with " +
                                    "a version of the runtime that is incompatible with this version of the " +
                                    "ServerDocument class.");
                    return;
                }

                if (ServerDocument.IsCacheEnabled(documentPath))
                {
                    serverDocument1 = new ServerDocument(documentPath);
                    System.Text.StringBuilder stringBuilder1 =
                        new System.Text.StringBuilder();

                    // Display all of the cached data items
                    // in the document.
                    foreach (CachedDataHostItem hostItem1 in
                             serverDocument1.CachedData.HostItems)
                    {
                        stringBuilder1.Append("\nNamespace and class: ");
                        stringBuilder1.Append(hostItem1.Id + "\n");
                        foreach (CachedDataItem dataItem1 in
                                 hostItem1.CachedData)
                        {
                            stringBuilder1.Append("     Data item: ");
                            stringBuilder1.Append(dataItem1.Id + "\n");
                        }
                    }
                    MessageBox.Show(stringBuilder1.ToString());
                }
                else
                {
                    MessageBox.Show("The specified document does not have cached data.");
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
            }
            catch (UnknownCustomizationFileException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document has a file " +
                                                     "extension that is not supported by Visual Studio Tools for Office.");
            }
            finally
            {
                if (serverDocument1 != null)
                {
                    serverDocument1.Close();
                }
            }
        }