To create a web part you have to have an installed Microsoft SharePoint 2010 on your PC or on a server you are able to connect to it with Administrator privilege, also you need the latest version of Microsoft Visual Studio 2010.
If you have the above mentioned applications then you are totally ready to start your first and real Visual Web Part.
In this tutorial we will start with develop a complete web part that can read data from SharePoint list, and in the coming posts we will develop a Web Part that read from Excel file uploaded already to SharePoint Document Library, then write the data to a different list.
Note: This Sample assume that you are working in the SharePoint Server or you are using Windows 7 as your development environment.
From the Start menu Start Microsoft Visual Studio 2010.
On the File menu, click New Project or Ctrl + N. New Window will come up to select your template
New Project Template
In Project Types, under Visual Basic, Open SharePoint menu and select 2010.
From available Project Types select “Visual Web Part”, you should see in description now “Type: Visual Basic, A project for creating a SharePoint visual web part”.
Type Sample1WebPart as the project name. Click OK.
In the SharePoint Customization Wizard, type your SharePoint server URL and validate it is correct, choose Deploy as a sandboxed or as a Farm solution. Click Finish.
In the Solution Explorer, right click on the “Visual WebPart1” (Created by default) and select “Delete” then press OK.
In the Solution Explorer, right click on the Sample1WebPart project and select Add, New Item.
In the Add New Item dialog, select the Web Part template and type Sample1WPas the Name. Click Add. (Recommended to have different name for the Web Part than the Project Name).
Now your project “SharePoint visual Web Part” is ready to be developed.
Visual Web Part in Visual Studio
In the Solution Explorer double click on “Sample1WPUserControl.ascx” to open it if it is not opened by default.
This is your area to design your interface (Note that wasn’t available in SharePoint 2007).
From the toolbox bar add “Button” and “Label”.
Select the Button, in Properties window point to ID and Change it to “BtnGet”.
Then Point to Text and change it to “Get Data”.
Repeat step 13, 14 but this time name the ID “LblRes” and Empty the Text.
Congratulation you finished your design phase.
Now right click anywhere in the page and select “View Code”, a new page will come up named “Sample1WPUserControl.ascx”.
From General Menu select “BtnGet” and from Declaration menu select Click. Now you should see a private sub completed for you.
Private Sub BtnGet_Click(ByVal sender As Object,ByVal e AsSystem.EventArgs)Handles BtnGet.Click
End Sub
Inside the above function we need to define:
SharePoint Site
Web
SharePoint List (We want it to read data from it)
SharePoint List item collection (used to put the return data from the list)
SharePoint query (specify the query)
Note: 5 are not used in this sample but it is required if you need to read a specific data
Define Variables:
Dim SPListVar As SPList ‘SharePoint List
Dim SPColl As SPListItemCollection‘Define a list item Collection
Using Site1 As New SPSite(Me.Context.Request.Url.ToString)‘Define the site
Using Web1 As SPWeb = Site1.OpenWeb ‘Define the web
SPListVar = Web1.Lists(“TmpList“) ‘Point to the required list
End Using
End Using
Now we need to read data from the defined list.
SPColl = SPListVar.GetItems() ‘ Fill the List item collection with the return data
Dim i As Integer
After retrieving the data we need to fill it in our Label control.
While i< SPColl.Count
Label1.Text=Label1.Text+ SPColl.Item(i).Item(“Title”).ToString+“<BR>”
‘Read every record and put it in a new line in the Label control
i= i + 1
End While
The full function code should look like:
Private Sub BtnGet_Click(ByVal sender As Object,ByVal e AsSystem.EventArgs)Handles BtnGet.Click
Dim SPListVar As SPList ‘SharePoint List
Dim SPColl As SPListItemCollection‘Define a list item Collection
Using Site1 As New SPSite(Me.Context.Request.Url.ToString)‘Define the site
Using Web1 As SPWeb = Site1.OpenWeb ‘Define the web
SPListVar = Web1.Lists(“TmpList“) ‘Point to the required list
End Using
End Using
SPColl = SPListVar.GetItems() ‘ Fill the List item collection with the return data
Dim i As Integer = 0
While i< SPColl.Count
Label1.Text = Label1.Text + SPColl.Item(i).Item(“Title”).ToString + “<BR>”
‘Read every record and put it in a new line in the Label control
i= i + 1
End While
End Sub
If your code look like the above then your web part is ready to be deployed
To deploy and test the web part:
Press F5 to deploy the Web Part.
When the page has opened in the browser, click the Edit button on the Page tab to put the page into edit mode.
On the Insert tab inside the Editing Tools contextual group, click the Web Partbutton.
In the Categories list, choose Custom and then choose Sample1WP in the Web Parts list. Click Add. The Web Part will appear in the page and display the interface.
Add the Visual Web Part
To Test it just click the button and see the result.
Now the Web Part is displaying the user-defined value inside the Web Part.
Check “how to deploy SharePoint 2010 custom web part” for a detailed installation on SharePoint Server.
To debug your code simply create a breakpoint in your code and repeat the deployment steps, the only difference now is when you click on the button the code will stop in the breakpoint in the Visual Studio and you can trace it as if it is a normal project.
but no one has an access for other user’s data, so there is only two solutions in this case:
* Either to create a custom security for each record in that list which is a very hard to do especially if this list keeps updating. As well this solution is not applicable in many cases like field or column security.
* Or to not allow anyone to access this list and create a SharePoint Web Part with special security to let the user read his own data only.
In this post we will enhance the previous post by adding a security code to our web part, which gives the end user the ability to read data from a list even if he/she doesn’t have an access to this list. To do so all we need is to add our magical line in the right place and that’s it, but before that we will describe what and how and where.
We need to use a property called “RunWithElevatedPrivileges” which is inherited from SPSecurity Class like this:
SPSecurity.RunWithElevatedPrivileges(New SPSecurity.CodeToRunElevated(AddressOf
functionName))
Yes it is as simple as this but we have to take in consideration that the function name should not take any parameters or return any.
Now we will use the above code in our web part “Sample1” and see how we can use it.
Instead of writing the code body immediately inside our BtnGet_Click(Check Post1) we will but that code in a separate sub, so our code will have a new sub called “ReadData” and it will look like the following:
Sub ReadData()
Dim SPListVar As SPList ‘SharePoint
List
Dim SPColl As SPListItemCollection ‘Define a list item Collection
Using Site1 As New SPSite(Me.Context.Request.Url.ToString)
‘Define the site
Using Web1 As SPWeb =Site1.OpenWeb ‘Define the web
SPListVar = Web1.Lists(“TmpList“) ‘Point to the required list “TmpList” is the list name in your SharePoint server.
End Using
End Using
SPColl = SPListVar.GetItems() ‘ Fill the List item collection
with the return data
Dim i As
Integer = 0
While i < SPColl.Count
Label1.Text = Label1.Text + SPColl.Item(i).Item(“Title”).ToString + “<BR>”
‘Read every record and put it in a new line in the Label
control
i= i + 1
End While
End Sub
Now we need to add our security code to BtnGet_Click so it will look like the following:
Private Sub BtnGet_Click(ByVal sender As
Object, ByVal e As System.EventArgs) Handles BtnGet.Click
SPSecurity.RunWithElevatedPrivileges(New SPSecurity.CodeToRunElevated(AddressOfReadData))
End Sub
Deploy and test the web part with user doesn’t have “read access” to our list and see how it can read the data. For more information regarding deploying the Web Part on SharePoint 2010 server check this detailed articles “Deploy SharePoint 2010 custom web parts“