web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

Creating Sessions PCF Control

(3) ShareShare
ReportReport
Posted on by 8
Hi, I am attempting to create a new session using the code below.  When I press my button, it will open and create a new session.  I would like to be able to add slugs for my agent scripts, but it does not appear to work.
 
 
private async createSession(params: string): Promise<IOutputs> {
        try {
            console.log("Params received in createSession:", params); // Log params
            const json = JSON.parse(params);
            const context = new Map();
            context.set("parametersStr", json.parametersStr);
    
            if ((window.parent as any).Microsoft.Apm) {
                return await (window.parent as any).Microsoft.Apm.createSession({
                    templateName: json.templateName,
                    sessionContext: context,
                    isFocused: json.isFocused
                }).then(
                    (_: any) => {
                        console.log("Session created successfully:", _);
                        return {
                            success: true,
                            message: '',
                            data: _
                        };
                    },
                    (_: any) => {
                        console.error("Error creating session:", _.message);
                        return {
                            success: false,
                            message: _.message,
                            data: ''
                        };
                    }
                );
            } else {
                console.error("Microsoft.Apm not found in window.parent.");
                return this._apmNotFound;
            }
        } catch (error) {
            console.error("Error in createSession:", error);
            return {
                success: false,

                data: undefined
            };
        }
    }
 
 
Code I am attempting to add to create the custom slugs.
const fullName = Xrm.Utility.getGlobalContext().userSettings.userName;
const currentUserName = fullName.split(' ')[0]; // Extract first name
sessionContext.set("currentUserName", currentUserName);

Checking for it using Microsoft.Apm.getFocusedSession().getContext(); it is not available.  The params show up in the console.log, but it is not accessible via the scripts.
 
Original code for the PCF control: https://pcf.gallery/xrmjs/
 
Thanks for any suggestions!
Categories:
I have the same question (0)
  • Suggested answer
    DB-19031402-0 Profile Picture
    2 on at
  • Suggested answer
    Dharanidharan Profile Picture
    638 User Group Leader on at
    It looks like you are correctly trying to pass the sessionContext with custom slugs (currentUserName), but the issue may be due to how the session context is set or how it is being retrieved.
     
    Possible Issues and Fixes
    Ensure Properly Setting sessionContext
    • Your current implementation initializes sessionContext as a Map(), but Microsoft.Apm.createSession() expects an object, not a Map.
    • Instead of Map(), try using a plain JavaScript object.

    Fix: Use a plain object for sessionContext instead of Map()

    private async createSession(params: string): Promise<IOutputs> {
        try {
            console.log("Params received in createSession:", params); // Log params
            const json = JSON.parse(params);
    
            // Create session context as an object (instead of Map)
            const sessionContext: Record<string, any> = {};
            sessionContext["parametersStr"] = json.parametersStr;
    
            // Add custom slug (currentUserName)
            const fullName = Xrm.Utility.getGlobalContext().userSettings.userName;
            const currentUserName = fullName.split(' ')[0]; // Extract first name
            sessionContext["currentUserName"] = currentUserName;
    
            console.log("Session Context:", sessionContext); // Debugging
    
            if ((window.parent as any).Microsoft.Apm) {
                return await (window.parent as any).Microsoft.Apm.createSession({
                    templateName: json.templateName,
                    sessionContext: sessionContext, // Pass as object
                    isFocused: json.isFocused
                }).then(
                    (_: any) => {
                        console.log("Session created successfully:", _);
                        return {
                            success: true,
                            message: '',
                            data: _
                        };
                    },
                    (_: any) => {
                        console.error("Error creating session:", _.message);
                        return {
                            success: false,
                            message: _.message,
                            data: ''
                        };
                    }
                );
            } else {
                console.error("Microsoft.Apm not found in window.parent.");
                return this._apmNotFound;
            }
        } catch (error) {
            console.error("Error in createSession:", error);
            return {
                success: false,
                message: error.message,
                data: undefined
            };
        }
    }
    
     
    Verify Retrieval of sessionContext

    After creating the session, ensure you're retrieving the session context correctly using:
    const session = Microsoft.Apm.getFocusedSession();
    console.log("Focused Session Context:", session.getContext());
    
    Check for Timing Issues

    Sometimes, the sessionContext might not be immediately available. Try adding a small delay before checking:
    setTimeout(() => {
        const session = Microsoft.Apm.getFocusedSession();
        console.log("Focused Session Context after delay:", session.getContext());
    }, 1000);
    
    Let me know if this helps or if you need further debugging!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Subra Profile Picture

Subra 128

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 99 Super User 2026 Season 1

#3
11manish Profile Picture

11manish 86

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans