Jan 6, 2012

Creating and Deploying a Visual Web Part using Microsoft Visual Studio 2010

To know about the Basics of Visual web part, read this article before knowing how to create and deploy the visual web part using visual studio 2010.
We will create a Visual web part for uploading the document to the SharePoint document library.
At the end of this article, we will learn
     1.How to create and deploy a Visual Web part
     2.How to programmatically upload a document to the SharePoint Document library

Open the Visual Studio and create a Visual Web Part project as shown below

When you click OK, you will see a pop-up window which will ask for the SharePoint site where the web part is going to be deployed as shown below. Hence give the proper URL of the SharePoint site. Also select the option Deploy as a farm solution because, visual web part is not a sandboxed solution

Once clicked Finish, you will see the project created with all the necessary files (assembly, .ascx files, feature to be deployed, .web part file) for a web part in the solution explorer as shown below

Click on the .ascx file and in the design mode, drag and drop the controls (FileUpload Control and Button) as shown below and complete the design part
I have added a JavaScript code which we used in this article for validating the file being uploaded is only .doc and .docx file.

Now, we will go for the coding the functionality of the web part. I have added a code in the Button1_Click event (Upload) of the web part as shown below

In the above code, give the proper name of the Document Library. I have used the default document library “Documents”.
Now Build the application and check for the errors. Once the build is successful, our web part is ready to be deployed in the SharePoint site.
It is very easy to deploy the visual web part. See the screenshot below.

That’s it. We have deployed our solution to the SharePoint site, which we have mentioned during creating the project.
Now, we will use the web part in our share point page.
Go to the SharePoint page -> Edit Page -> Insert web part -> Categories -> Custom -> Select the web part created (in our case I have named visualwebpart1)

Once added, we will see our web part designed in the SharePoint site page.

I will select a file and click upload. The file will get uploaded into the document library mentioned in the code.

Once Clicked Upload, the document is uploaded into the document library as shown below.

That’s it! We are done. We have learned to create and deploy the visual web part in SharePoint 2010.


7 comments:

  1. Can you post your full files else where since MegaUpload got shut down.

    ReplyDelete
    Replies
    1. Hi,
      I have delinked the old file and shared it in the another File Share and now you can download the whole solution.

      Delete
  2. can you post download link

    ReplyDelete
    Replies
    1. There are some problems with the file share. I have shared it again. JHope this helps you!

      Delete
  3. When I create same visual web part for Sharepoitn 2013 When I upload File It shows Error Message : Web Part Error: Sandboxed code execution request failed. Correlation ID: c393e99c-d08b-1000-af65-940a5befbfc4.

    ReplyDelete
  4. would you upload file again?

    ReplyDelete
    Replies
    1. I doubt if I have the backup. I will check and upload it in a different File Share.

      Delete

Dear Readers,

I LOVE to hear from you! Your feedback is always appreciated. I will try to reply to your query as soon as possible.

1. Make sure to click the "Notify me" check box at the right side to be notified of follow up comments and replies.
2. Please Do Not Spam - Spam comments will be deleted immediately upon review.