Tuesday, 17 March 2015

Creating a first ASP.NET page

Assuming we have created a virtual directory for ASP.NET pages (Adding ASP.NET pages to a SharePoint Installation) we can now start adding ASP.NET pages.



  • Using Visual Studio, create a new web site project on the folder created in the previous step.


  • Add a Web Form (aspx page) to the folder:


  • Add some content to the page (eg "Hello World") and save the page
     
  • Verify that the page can be browsed using the SharePoint URL plus the new folder and name:









Adding SharePoint to our project


So far we have an ASPX page running on the SharePoint server, we now need to connect to the SharePoint system.


  • Open the Code Behind (.cs) file and add "Using Microsoft.Sharepoint"






  • This will cause an error (red line) initially as we need to add the reference to the SharePoint DLL on the server.
     
  • Add a reference to your project:  Right click the project in the solution tree, and select "Add Reference", choose the "Browse" option and browse to the SharePoint DLL on the SharePoint server.







The Sharepoint DLL will be in c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI   (note \15\ is for Sharepoint 2013, use \14\ for SP 2010 etc)


  • Select "Microsoft.Sharepoint.dll" and click "Add" to add the reference


  • This will add several DLLs to the projects "Bin" folder and should clear the error on "Using Microsoft.Sharepoint" (after a pause)






NB  This only needs to be done once for the project, you don’t need to repeat this for every page.


Testing the SharePoint link


  • Add a literal to the default.aspx page:


  • Add code to the code behind Page_Load event:




Insert the URL of your Sharepoint site in the new SPSite function.
This code accesses the SharePoint site (the root site, but it doesn’t matter which site, as long as it is a valid site on your SharePoint installation.

Next it open the SPWeb object associated with this site and gets the current user from the web object.

The name of the current user is output to the literal.


  • Save the page and browse to (or refresh) the page:






This demonstrates connection to the SharePoint system.





No comments:

Post a Comment