C# code to upload files as web resources

By | September 12, 2017

 

This is not the most common development task by far, but that’s just one more reason to share the code since it took me a little while earlier today to assemble it from different sources.

Here is how you can upload all the files from the specified folder to Dynamics as web resources:

1. You’ll need to identify web resource type, so there will be a enum

        enum WEBRESOURCETYPE  
        {
            HTML = 1,
            CSS = 2,
            JS = 3,
            XML = 4,
            PNG = 5,
            JPG = 6,
            GIF = 7,
            XAP = 8,
            XSL = 9,
            ISO = 10
        }

2. You’ll need to be able to convert file contents to Base64

            static public string getEncodedFileContent(String pathToFile)
            {
                FileStream fs = new FileStream(pathToFile, FileMode.Open, FileAccess.Read);
                byte[] binaryData = new byte[fs.Length];
                long bytesRead = fs.Read(binaryData, 0, (int)fs.Length);
                fs.Close();
                return System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
            }

3. And, finally, you’ll need to use all that to upload the files to Dynamics

        static void UploadWebResources(IOrganizationService service, string folder, string resourcesPath)
        {
            var files = System.IO.Directory.GetFiles(folder, “*”);
            foreach (var f in files)
            {
                string webResourceName = resourcesPath + System.IO.Path.GetFileName(f);
                var content = getEncodedFileContent(f);
                Entity wr = new Entity(“webresource”);
                wr[“content”] = content;
                wr[“displayname”] = f;
                wr[“description”] = “Uploaded File”;
                wr[“name”] = webResourceName;
                bool createWr = true;
                switch (System.IO.Path.GetExtension(f))
                {
                    case “.js”:
                        wr[“webresourcetype”] = new OptionSetValue((int)WEBRESOURCETYPE.JS);
                        break;
                    case “.gif”:
                        wr[“webresourcetype”] = new OptionSetValue((int)WEBRESOURCETYPE.GIF);
                        break;
                    case “.css”:
                        wr[“webresourcetype”] = new OptionSetValue((int)WEBRESOURCETYPE.CSS);
                        break;
                    case “.html”:
                        wr[“webresourcetype”] = new OptionSetValue((int)WEBRESOURCETYPE.HTML);
                        break;
                    default:
                        createWr = false;
                        break;
                }
                if (createWr)
                {
                    QueryByAttribute qba = new QueryByAttribute(“webresource”);
                    qba.ColumnSet = new ColumnSet(true);
                    qba.AddAttributeValue(“name”, webResourceName);
                    if (service.RetrieveMultiple(qba).Entities.FirstOrDefault() == null)
                    {
                        service.Create(wr);
                    }
                }
            }

The code above will start by loading the list of files from the folder identified by one of the parameters. Then it will create a new webresource record for each of the files, but it will also verify if the files has already been uploaded to Dynamics (in which case it will skip such a file.. but you might use “service.Update” if you wanted to update the web resource instead, just don’t forget to set wr.Id if that’s the case)

6 thoughts on “C# code to upload files as web resources

  1. Niels Steenbeek

    Nice article, which was very usefull. Did you also manage to add to a specific solution instead of the default one (“Active”)? Specifying the solutionid attribute did not work on my side.

    Reply

Leave a Reply to Niels Steenbeek Cancel reply

Your email address will not be published. Required fields are marked *