Using outside Objects in a Angular2 Service

259 views
Skip to first unread message

Kameron Berget

unread,
Aug 20, 2016, 12:11:48 PM8/20/16
to AngularJS
I am building some components that work inside of SharePoint. Every SharePoint page provides an object on page load called _spPageContextInfo that provides details about the user, the site, etc. When I attempt to use this object in an Angular2 service I get "Cannot find Name". Is there anything I can do inside the code to get rid of the error? It should be fine at runtime I am just sick of seeing errors everywhere.

Sander Elias

unread,
Aug 21, 2016, 10:16:54 PM8/21/16
to AngularJS
Hi Kameron,

Wrap it inside a service. If you are using typescript, you can create an interface for it, and use that wherever you need it.

Regards
Sander

Kameron Berget

unread,
Aug 22, 2016, 12:51:15 PM8/22/16
to AngularJS
Thank you. I have created an interface and imported it as shown below. Still getting errors.

// pagecontextinfo.ts

export interface _spPageContextInfo {
   hasManageWebPermissions
: boolean;
   isAnonymousGuestUser
: boolean;
   isAppWeb
: boolean;
   isSiteAdmin
: boolean;
   layoutsUrl
: string;
   pageItemId
: number,
   pageListId
: string;
   pagePersonalizationScope
: number;
   
ProfileUrl: string;
   serverRequestPath
: string;
   siteAbsoluteUrl
: string;
   siteServerRelativeUrl
: string;
   systemUserKey
: string;
   userId
: number;
   userLoginName
: string;
   webAbsoluteUrl
: string;
   webServerRelativeUrl
: string;
   webTitle
: string;
}

import { _spPageContextInfo } from "../sharepoint/pagecontextinfo";

src/app/services/spdata.service.ts(21,24): error TS2304: Cannot find name '_spPageContextInfo'.


Lucas Lacroix

unread,
Aug 22, 2016, 1:56:25 PM8/22/16
to AngularJS
You're declaring an interface, not a global.

Try this article:

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
--
Lucas Lacroix
Computer Scientist
Advanced Technology Division, MEDITECH

Sander Elias

unread,
Aug 23, 2016, 2:36:25 AM8/23/16
to AngularJS
Hi Kameron,

As Lucas pointed out, you not only need a interface, you also need to declare the var. I would wrap both in a simple service, so you can inject it where you need it, and if something changes in the future, you just have 1 place to fix/update in that case. 

Regards
Sander
Reply all
Reply to author
Forward
0 new messages