Create a webpart which will allow us to do some IO operations with the files and folders in client

Create a webpart which will allow us to do some IO operations with the files and folders in client

  • Comments 1

 

We all were using the FileInfo, DirectoryInfo & DriveInfo classes coming under System.IO namespace quite often while working with files and directories in server. But what we can do, if want to do some IO operations with files and directories in client side?

 

The only way would be using client side script consuming an Active X object Scripting.FileSystemObject

 

In order to execute the methods with this Active X object in a .html file, we have to enable the “Run Active X controls and plug-ins” – in our IE browser by going – Tools à Internet options à Security Tab à click on “Custom Level” button.

clip_image001

And if you want to execute those methods in an ASP.NET context then you have enable one more option which is “Initialize and script ActiveX controls not marked as safe”, otherwise we will get a JS error “Automation server can't create object”. Thus, we have to enable those 2 options for creating an instance of that Active X object in ASP.NET based applications. (please see the above image)

 

What I did that, I have created a pluggable control, which is nothing but a custom webpart for finding the drive information of client machine, creating a folder, file and writing contents to that file and reading it and an option to delete the folder which I had created. Since these type of functionalities will create some sort of security breaches, we need to handle it very carefully.

 

Below is a screen shot of my custom webpart in a SharePoint application.

image

Actually, I have created a web usercontrol and implemented that functionality. Here I am using my webpart control to just load that usercontrol and display it in the browser. After designing the UI and implementing the JS code I just placed my usercontrol inside the _layouts folder.

First we can see the functionality of this usercontrol. It has some html controls in the UI. Whenever we click on the “Get Drive information” button, it will populate the names of the working drives in a dropdown list. Once if we select any drives then it will display the Total size of that drive and the available free size.

Once we select any drive, then we will be getting an option to create folder and then a file and add some content to that file in the preceding controls. It is very self descriptive and user friendly. Also, at the end, you will be getting a functionality to read the data from the file that you have created and an option to delete the folder that you created.

 

Once your usercontrol is ready then you can display it in your SharePoint site using a custom webpart. Here is the code for the custom webpart. Here I am keeping my usercontrol under _layouts folder.     

  protected override void CreateChildControls()

        {

            try

            {

                base.CreateChildControls();

                this.Controls.Clear();

                oUserControl = (UserControl)Page.LoadControl("/_layouts/ClientIOJSUtility.ascx");

                this.Controls.Add(oUserControl); 

            }

            catch (Exception ex)

            {

               System.Web.HttpContext.Current.Response.Write(ex.Message);

            }

           

I have attached the webpart code and the usercontrol with this post.

For more information about Scripting.FileSystemObjectplease refer the below MSDN link :http://msdn.microsoft.com/en-us/library/bkx696eh(VS.85).aspx

Attachment: JSUtility.zip
Comments
  • Recently, I worked a tricky and interesting case where a customer wanted to “silently” put a file in

Page 1 of 1 (1 items)
Leave a Comment
  • Please add 4 and 7 and type the answer here:
  • Post