Tell Me More: Get Context in your Canvas App

The Force.com Canvas SDK provides calls and objects that let you retrieve context information about the application and the current user from Salesforce.

Getting Context

When you authenticate your canvas app using signed request, you get the CanvasRequest object (which contains the Context object) as part of the POST to the canvas app URL. If you’re authenticating using OAuth, or if you want to make a call to get context information, you can do so by making a JavaScript call. You can use this information to make subsequent calls for information and code your app so that it appears completely integrated with the Salesforce1 user interface.

The following code sample is an example of a JavaScript call to get context. This code creates a link with the text “Get Context” which then calls the Sfdc.canvas.client.ctx function.

1swfobject.registerObject("clippy.codeblock-0", "9");<script>
2    function callback(msg) {
3       if (msg.status !== 200) {
4          alert("Error: " + msg.status);
5          return;
6       }
7       alert("Payload: ", msg.payload);
8    }
9                
10    var ctxlink = Sfdc.canvas.byId("ctxlink");
11    var client = Sfdc.canvas.oauth.client();
12    ctxlink.onclick=function() {
13       Sfdc.canvas.client.ctx(callback, client)};
14    }
15</script>
16
17<a id="ctxlink" href="#">Get Context</a>
18

Context Objects

When you make a call to get context in your canvas app, you get a CanvasRequest object back in the response. This object contains all the contextual information about the application and the user. The context objects include:
Object Description
CanvasRequest Returns the Context and Client objects.
Client Returns context information about the client app.
Context Returns information about the consumer of the canvas app. Contains the Application, Environment, Links, Organization, and User objects.
Application Returns information about the canvas app, such as version, access method, URL, and so on.
Environment Returns information about the environment, such as location, UI theme, and so on.
Links Returns links, such as the metadata URL, user URL, Chatter groups URL, and so on. You can use these links to make calls into Salesforce from your app.
Organization Returns information about the organization, such as name, ID, currency code, and so on.
User Returns information about the currently logged-in user, such as locale, name, user ID, email, and so on.
This code snippet shows an example of the CanvasRequest object:
1{
2    "context":
3    {
4        "application": 
5        {
6            "applicationId":"06Px000000003ed", 
7            "authType":"SIGNED_REQUEST", 
8            "canvasUrl":"http://instance.salesforce.com:8080
9                /canvas_app_path/canvas_app.jsp", 
10            "developerName":"my_java_app", 
11            "name":"My Java App", 
12            "namespace":"org_namespace", 
13            "referenceId":"09HD00000000AUM", 
14            "samlInitiationMethod": "None",
15            "version":"1.0.0"
16        },
17        "user":
18        {
19            "accessibilityModeEnabled":false, 
20            "currencyISOCode":"USD", 
21            "email":"admin@6457617734813492.com", 
22            "firstName":"Sean", 
23            "fullName":"Sean Forbes", 
24            "isDefaultNetwork":false, 
25            "language":"en_US", 
26            "lastName":"Forbes", 
27            "locale":"en_US", 
28            "networkId":"0DBxx000000001r", 
29            "profileId":"00ex0000000jzpt", 
30            "profilePhotoUrl":"/profilephoto/005/F", 
31            "profileThumbnailUrl":"/profilephoto/005/T", 
32            "roleId":null, 
33            "siteUrl":"https://mydomain.force.com/", 
34            "siteUrlPrefix":"/mycommunity", 
35            "timeZone":"America/Los_Angeles", 
36            "userId":"005x0000001SyyEAAS", 
37            "userName":"admin@6457617734813492.com", 
38            "userType":"STANDARD"
39        }, 
40        "environment": 
41        { 
42            "parameters":
43            {
44                "complex":
45                {
46                    "key1":"value1",
47                    "key2":"value2"
48                },
49                "integer":10,
50                "simple":"This is a simple string.",
51                "boolean":true
52            },
53            "dimensions": 
54            { 
55                "height": "900px", 
56                "width": "800px", 
57                "maxHeight":"2000px",
58                "maxWidth":"1000px",
59
60                "clientHeight":"80px",
61                "clientWidth":"968px"
62            }, 
63            "displayLocation":"Chatter",
64            "locationUrl": "http://www.salesforce.com
65                /some/path/index.html", 
66            "uiTheme":"Theme3",
67            "record":{},
68            "version": 
69            { 
70                "api":"31.0", 
71                "season":"SUMMER" 
72            }, 
73        }, 
74        "organization":
75        {
76            "currencyIsoCode":"USD", 
77            "multicurrencyEnabled":true, 
78            "name":"Edge Communications", 
79            "namespacePrefix":"org_namespace", 
80            "organizationId":"00Dx00000001hxyEAA"
81        },
82        "links":
83        {
84            "chatterFeedItemsUrl":"/services/data/v31.0/
85                chatter/feed-items", 
86            "chatterFeedsUrl":"/services/data/v31.0/
87                chatter/feeds", 
88            "chatterGroupsUrl":"/services/data/v31.0/
89                chatter/groups", 
90            "chatterUsersUrl":"/services/data/v31.0/
91                chatter/users", 
92            "enterpriseUrl":"/services/Soap/c/31.0/
93                00Dx00000001hxy", 
94            "loginUrl":"http://login.salesforce.com",
95            "metadataUrl":"/services/Soap/m/31.0/00Dx00000001hxy", 
96            "partnerUrl":"/services/Soap/u/31.0/00Dx00000001hxy", 
97            "queryUrl":"/services/data/v31.0/query/", 
98            "recentItemsUrl":"/services/data/v31.0/recent/", 
99            "restUrl":"/services/data/v31.0/", 
100            "searchUrl":"/services/data/v31.0/search/", 
101            "sobjectUrl":"/services/data/v31.0/sobjects/", 
102            "userUrl":"/005x0000001SyyEAAS" 
103        }
104    },
105    "client":
106    {
107        "instanceId":"06Px000000002JZ", 
108        "instanceUrl":"http://instance.salesforce.com:
109            8080", 
110        "oauthToken":"00Dx0000X00Or4J!ARQAKowP65p8FDHkvk.Uq5...", 
111        "targetOrigin":"http://instance.salesforce.com:
112            8080"
113    }, 
114"algorithm":"HMACSHA256", 
115"userId":"005x0000001SyyEAAS", 
116"issuedAt":null
117}
For more information about context objects and the Force.com Canvas SDK, see the Force.com Canvas Developer’s Guide.