Monday, March 19, 2012

How to build web service and use web service in sharepoint

How to build web service and push on sharepoint

Open visual studio 2010 | New Project
On Web template, choose .NET Framework 3.5 from dropdownlist then choose ASP.NET Web Service Application project and provide name is HelloWorld
Structure of project as follows
Open Service1.asmx.cs and view default code
Typing F5 and click to method HelloWorld
Click Invoke
Result as follow
Right click Project | properties
Choose Category Application then click to button Assembly information …
Fill full information, then click ok
Continue, choose category Signing
Check to checkbox “Sign the assembly” and choose new  strong name key from dropdownlist below
Input name’s key file name and uncheck “Protect my key file with a password”
Copy Dll in bin folder of your project (drag and drop to bin folder to assembly folder)
After copy dll to here, right click to .dll | properties
Copy Public token key below and paste anywhere example notepad
Open Service1 by notepad (Image 15)
You will see class as follows
Append this segment code “,
 HelloWord, Version=, Culture=neutral, PublicKeyToken=140f54b64af2de0f”
 in class as follow
Copy service1 to folder “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS” as follows
Open visual studio command prompt (2010) and input  cd c:\ and type enter
Input: Disco http://quochung-axioo:90 /_layouts/Service1.asmx
After Disco successful, automatically the service1.asmx will push on sharepoint
Type link http://quochung-axioo:90 /_layouts/Service1.asmx on address bar and see result as follows

How to call web service by web part

New Visual Webpart
Choose “Deploy as a farm solution”
Right click to reference | Add Service Reference …
Click button Advanced…
Click button Add Web Reference…
Paste “http://quochung-axioo:90 /_layouts/Service1.asmx” to URL then click button GO
Login by administrator account of sharepoint
Click Add reference

Web services was added
Open visualWebpartUserControl.ascx  | drag and drop literal to here
Copy this segment code to Page_Load event as follows:

            HelloWordService.Service1 helloWord = new HelloWordService.Service1();
            Literal1.Text= helloWord.HelloWorld();

Build and deploy project you will meet error
How to solve
Go to Central  Admin | click to “Manage web applications” link then click to your web application – On ribbon choose Icon Authentication Provides  (Image 35)
Dialog appear, click to “Default” link
Check to “Enable anonymous access”
Back to list your application, on you web application choose Icon Anonymous Policy on ribbon
In anonymous User Policy:  choose radio button “None - policy”
Go to your web application
Site Actions | Site Settings | Site Permissions
On ribbon click Icon Anonymous Access
In Anonymous user can access: choose “Entire Web site”
Refresh page which contain webpart call web service and you see result display as follows


  1. This is very good website, thank you for the post Custom Web Services Custom Web Services
    Custom Web Services-Socialsweep is an affordable internet marketing company that offers social media marketing and custom web services. Get search engine optimization and professional website services done here. We have good search engine optimization and a professional web services team that are friendly and easy to work with