IT tutorials
 
Applications Server
 

Developing, Integrating, and Building Applications in Sharepoint 2013 (part 4) - Events and Logic Integration

1/12/2014 8:55:58 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

2.2 Events and Logic Integration

Providing a UI for users is usually the most prominent aspect of any application. However, responding to the actions users take either within an application, or to interact with an application, is also extremely important. SharePoint applications provide the ability to both respond to activities within your application (such as a button click) and respond to activities within SharePoint (such as a document being checked out).

Responding to activities within your application is very straightforward. Your application’s UI and code run remotely from SharePoint and are simply surfaced via App Parts and Pages. For that reason, responding to an event such as a button being clicked in your application is entirely tied to your application’s programming framework. For example, if your app is built with ASP.NET then you simply catch the OnClick event for an ASP.NET button. SharePoint does not get in your way for these types of events.

The following exercise shows you how to add code behind a button in your SharePoint application that responds when it is pressed.


Responding to Events Within Your Application (EventsandLogicintegreation.zip)

In this exercise you add a button to your application and include code that is run when it is pressed. You must have completed the “Building Your First SharePoint Application” in the “App Parts and Pages” section before starting this example.
1. Ensure you have the MyFirstSharePointApp solution open in Visual Studio 2012.
2. Open the Default.aspx file under the MyFirstSharePointAppWeb project.
3. Insert the following code directly after </div>.
<asp:Button ID="Button1" runat="server" Text="Do Something" 
OnClick="Button1_Click" /><br />
<asp:Label ID="txtUser" runat="server" Text=""></asp:Label>
4. Open the Default.aspx.cs file.
5. Replace the Page_Load method with the following code:
protected void Page_Load(object sender, EventArgs e)
{
var contextToken = TokenHelper.GetContextTokenFromRequest(Page.Request);
var hostWeb = Page.Request["SPHostUrl"];

if (!IsPostBack)
{
Button1.CommandArgument = contextToken;


using (var clientContext =
TokenHelper.GetClientContextWithContextToken(hostWeb, contextToken,
Request.Url.Authority))
{
clientContext.Load(clientContext.Web, web => web.Title);
clientContext.ExecuteQuery();
Response.Write(clientContext.Web.Title);
clientContext.ToString();
}
}
}
The Button1.CommandArgument = contextToken; line ensures that the contextToken is stored for later use on postback events when the button is clicked. You need it to make subsequent CSOM calls.
6. Add the following new method after the Page_Load method:
protected void Button1_Click(object sender, EventArgs e)
{
var contextToken = ((Button)sender).CommandArgument;
var hostWeb = Page.Request["SPHostUrl"];

using (var clientContext =
TokenHelper.GetClientContextWithContextToken(hostWeb, contextToken,
Request.Url.Authority))
{
clientContext.Load(clientContext.Web.CurrentUser);
clientContext.ExecuteQuery();

txtUser.Text = clientContext.Web.CurrentUser.LoginName;
}
}
7. Press F5 to package and deploy your application.
8. After your app deploys, add your app’s App Part to the homepage of your site by clicking Page ⇒ Edit ⇒ Insert ⇒ App Part. Select MyAppPart from the list and then click Add.
9. Inside your App Part you should now see a button called Do Something. Click it.
10. You should now see your full login name displayed directly below the button, as shown in Figure 14.

FIGURE 14

image

How It Works

In this exercise you added a button to your application and responded to it being clicked. Nothing is particularly special about this button over and above a standard ASP.NET button; however, it is worth understanding the role of the contextToken and the need to keep it between postbacks to your code so that further calls to the CSOM can be made. In the exercise you stored it in the CommandArgument for the button; however, the same could have been achieved in a variety of other ways, such as a hidden control on the page. The important thing is that it is in a location that you are able to retrieve at a later time during a postback event — you need this context in order to call SharePoint via the CSOM on subsequent requests.

For responding to events that occur inside SharePoint, such as a document’s being saved or updated, SharePoint provides event receivers. In SharePoint 2010 developers could use event receivers to receive events from SharePoint when a user took certain actions such as checking in a document or saving a list item. SharePoint 2013 also provides event receivers that allow applications to respond to events occurring within a SharePoint site. The primary difference from SharePoint 2010 is that event receivers now trigger code that runs remotely outside the SharePoint process in your application’s code. They do this via a commonly defined Web service interface. This means the code that you want to respond to events could reside in almost any system you like, as long as it is callable over HTTP/HTTPS from the SharePoint Server or Office 365. An example might be your application code that runs in Windows Azure.

 
Others
 
- Developing, Integrating, and Building Applications in Sharepoint 2013 (part 3) - User Interface Integration - Ribbon and Action Menus
- Developing, Integrating, and Building Applications in Sharepoint 2013 (part 2) - User Interface Integration - App Parts and Pages
- Developing, Integrating, and Building Applications in Sharepoint 2013 (part 1)
- Microsoft Lync Server 2013 Front End Server : Installing the Front End Role
- Microsoft Lync Server 2013 Front End Server : Installation - Topology Builder for Standard Edition Deployments
- Microsoft Lync Server 2013 Front End Server : Installation - Lync Server 2013 Topology Builder
- Microsoft Lync Server 2013 Front End Server : Active Directory Preparation
- Sharepoint 2013 : Understanding SharePoint app model architecture (part 7) - Working with app user-interface entry points - Building UI custom actions
- Sharepoint 2013 : Understanding SharePoint app model architecture (part 6) - Working with app user-interface entry points - Building app part
- Sharepoint 2013 : Understanding SharePoint app model architecture (part 5) - Setting the start page URL, Understanding the app web
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us