Tuesday, July 18, 2023

Updating a global variable property

 Suppose defaults is our global variable


Set(defaults,Patch(defaults,{

    font:Self.Font}));

Using SVG Images in PowerApps

 




"data:image/svg+xml;utf8, " & EncodeUrl(

//Set Map svg text

Substitute("<svg xmlns='http://www.w3.org/2000/svg' height='24' width='24'><path d='M7 22H9V24H7V22M11 22H13V24H11V22M15 22H17V24H15V22M5 4H19A2 2 0 0 1 21 6V18A2 2 0 0 1 19 20H5A2 2 0 0 1 3 18V6A2 2 0 0 1 5 4M5 8V18H19V8H5'/></svg>", "<svg ", "<svg fill='"&defaults.primaryColorHex&"' "))

Thursday, July 6, 2023

Upload file to doc library using graph API

 Set(

    _showLoading,

    true

);

With(

    // you'll want to update these initial with variables for your scenario

    {

        // eg: "https://contoso.sharepoint.com/sites/operations" (Note: Do not include a trailing slash.)

        wthSharePointSiteUrl: _appSite,

        // eg: "Audit Files" (Note: Use the Display Name of the Document Library)

        wthDocumentLibraryName: _appDocLibraryName,

        // eg: "First(AttachmentsControl).Name" || "samplefolder/hello-world.docx"

        wthFileName: GUID()&".jpg",

        // eg: "First(AttachmentsControl).Value" (Note: The Graph API currently limits us to files of 4MB or less.)

        wthFileContent: AddMediaButton2.Media

    },

    // you can leave the remaining logic alone unless you know what you're doing or don't mind breaking things ☺

    With(

        {

            // resolve the host name from the provided site url (eg: "https://contoso.sharepoint.com/sites/operations" => "contoso.sharepoint.com")

            wthHostName: $"{Index(

                Split(

                    Index(

                        Split(

                            wthSharePointSiteUrl,

                            "https://"

                        ),

                        2

                    ).Value,

                    ".com"

                ),

                1

            ).Value}.com",

            // resolve the site's relative path from the provided site url (eg: "https://contoso.sharepoint.com/sites/operations" => "/sites/operations")

            wthServerRelativePath: $"{Index(

                Split(

                    wthSharePointSiteUrl,

                    ".com"

                ),

                2

            ).Value}"

        },

        Set(

            ServerRelativePath,

            wthServerRelativePath

        );

        With(

            {

                // get the site information from the graph api

                wthRequestBody: Office365Groups.HttpRequest(

                    $"https://graph.microsoft.com/v1.0/sites/{wthHostName}:{wthServerRelativePath}?$select=id",

                    "GET",

                    ""

                )

            },

            With(

                {

                    // resolve the site id from the response's id field (eg: "contoso.sharepoint.com,6fcfe1d9-faf5-451f-8a91-ded0005629ed,57f57702-0535-47a1-b377-0a36ba65f0d8" => "6fcfe1d9-faf5-451f-8a91-ded0005629ed")

                    wthSiteId: Index(

                        Split(

                            Text(wthRequestBody.id),

                            ","

                        ),

                        2

                    ).Value

                },

                With(

                    {

                        // get a list of document libraries from the provided site

                        wthRequestBody_drives: Office365Groups.HttpRequest(

                            $"https://graph.microsoft.com/v1.0/sites/{wthSiteId}/drives?$select=id,name",

                            "GET",

                            ""

                        )

                    },

                    Set(

                        SiteId,

                        wthSiteId

                    );

                    With(

                        // parse the request response into a two-column table (id, name) while casting each field to text

                        {

                            wthAllDrives: ForAll(

                                Table(wthRequestBody_drives.value),

                                {

                                    id: Text(ThisRecord.Value.id),

                                    name: Text(ThisRecord.Value.name)

                                }

                            )

                        },

                        Set(

                            AllDrives,

                            wthAllDrives

                        );

                        With(

                            // lookup the id for the document library whose name maches the provided document library name

                            {

                                wthDriveId: LookUp(

                                    wthAllDrives,

                                    name = wthDocumentLibraryName

                                ).id

                            },

                             // Notify(wthFileContent);

                            With(

                                // put the new file content into the document libary using the provided name and file content

                                {

                                    wthRequestBody_upload: Office365Groups.HttpRequest(

                                        $"https://graph.microsoft.com/v1.0/sites/{wthSiteId}/drives/{wthDriveId}/root:/{wthFileName}:/content",

                                        "PUT",

                                        wthFileContent

                                    )

                                },

                                UpdateContext({ctxUploadedFileUrl: Text(wthRequestBody_upload.webUrl)});

                                UpdateContext({ctxUploadedTitle: Text(wthRequestBody_upload.name)});

                                

                                UpdateContext({ctxUploaded: wthRequestBody_upload});

                                

                                UpdateContext({ctxUploadedID:LookUp(Avatars, 'Link to item' = ctxUploadedFileUrl).ID});

                                Patch(Avatars,LookUp(Avatars, ID = ctxUploadedID),{

                                    IsCustom:"Yes"

                                });

                                Patch(Employees,First(Filter(Employees, EmployeeID = _userID)),{

ProfileIcon:ctxUploadedID

                                });

                            )

                        )

                    )

                )

            )

        )

    )

);

Set(

    _showLoading,

    false

);


Push notifications

    If(ThisItem.PostEmployeeID <> _userID ,UpdateContext({var_dynParams : JSON({postID : ThisItem.PostID})});

    UpdateContext({poster:First(Filter(Employees,EmployeeID = ThisItem.PostEmployeeID)).Email}); 

PowerAppsNotificationV2.SendPushNotificationV2("PowerApps",JSON({ appIdentifier: _appID }),[poster],_userDisplayName & " loved your post ❤️",true, ParseJSON(var_dynParams)));

Tabs handling containers

 While using tabs in a gallery using button if you want to show hide container , use Button.Text as flag, the selected button text will work as condition to check which one is selected

Search using GRAPH API

 Set(

    _showLoading,

    true

);

If(Button3_16.Text = "People",UpdateContext({_path:_appSearchPeoplePath}),UpdateContext({_path:_appSearchPath}));

Clear(col_allSearchresultsPosts);

Clear(col_allSearchresultsPeople);

Set(var_pageNumber,1);

Set(var_pageSize,3);

Set(

    var_requestBody,

"{

    ""requests"": [

        {

            ""entityTypes"": [

                ""listItem""

            ],

            ""query"": {

                ""queryString"": """&txtComment_3.Text&" path:\"""&_path&"\""""

            },

            ""fields"": [

                ""title"",

                ""listItemId""

            ]

        }

    ]

}"

);

Set(

    _searchText,

    "data:text/plain;base64," & With(

        {

            InputText: (var_requestBody),

            AsciiTable: AddColumns(

                Sequence(

                    2 ^ 8,

                    1

                ),

                "char",

                Char(Value)

            ),

            B64ToBin: Table(

                {

                    b64: "A",

                    bin: "000000"

                },

                {

                    b64: "B",

                    bin: "000001"

                },

                {

                    b64: "C",

                    bin: "000010"

                },

                {

                    b64: "D",

                    bin: "000011"

                },

                {

                    b64: "E",

                    bin: "000100"

                },

                {

                    b64: "F",

                    bin: "000101"

                },

                {

                    b64: "G",

                    bin: "000110"

                },

                {

                    b64: "H",

                    bin: "000111"

                },

                {

                    b64: "I",

                    bin: "001000"

                },

                {

                    b64: "J",

                    bin: "001001"

                },

                {

                    b64: "K",

                    bin: "001010"

                },

                {

                    b64: "L",

                    bin: "001011"

                },

                {

                    b64: "M",

                    bin: "001100"

                },

                {

                    b64: "N",

                    bin: "001101"

                },

                {

                    b64: "O",

                    bin: "001110"

                },

                {

                    b64: "P",

                    bin: "001111"

                },

                {

                    b64: "Q",

                    bin: "010000"

                },

                {

                    b64: "R",

                    bin: "010001"

                },

                {

                    b64: "S",

                    bin: "010010"

                },

                {

                    b64: "T",

                    bin: "010011"

                },

                {

                    b64: "U",

                    bin: "010100"

                },

                {

                    b64: "V",

                    bin: "010101"

                },

                {

                    b64: "W",

                    bin: "010110"

                },

                {

                    b64: "X",

                    bin: "010111"

                },

                {

                    b64: "Y",

                    bin: "011000"

                },

                {

                    b64: "Z",

                    bin: "011001"

                },

                {

                    b64: "a",

                    bin: "011010"

                },

                {

                    b64: "b",

                    bin: "011011"

                },

                {

                    b64: "c",

                    bin: "011100"

                },

                {

                    b64: "d",

                    bin: "011101"

                },

                {

                    b64: "e",

                    bin: "011110"

                },

                {

                    b64: "f",

                    bin: "011111"

                },

                {

                    b64: "g",

                    bin: "100000"

                },

                {

                    b64: "h",

                    bin: "100001"

                },

                {

                    b64: "i",

                    bin: "100010"

                },

                {

                    b64: "j",

                    bin: "100011"

                },

                {

                    b64: "k",

                    bin: "100100"

                },

                {

                    b64: "l",

                    bin: "100101"

                },

                {

                    b64: "m",

                    bin: "100110"

                },

                {

                    b64: "n",

                    bin: "100111"

                },

                {

                    b64: "o",

                    bin: "101000"

                },

                {

                    b64: "p",

                    bin: "101001"

                },

                {

                    b64: "q",

                    bin: "101010"

                },

                {

                    b64: "r",

                    bin: "101011"

                },

                {

                    b64: "s",

                    bin: "101100"

                },

                {

                    b64: "t",

                    bin: "101101"

                },

                {

                    b64: "u",

                    bin: "101110"

                },

                {

                    b64: "v",

                    bin: "101111"

                },

                {

                    b64: "w",

                    bin: "110000"

                },

                {

                    b64: "x",

                    bin: "110001"

                },

                {

                    b64: "y",

                    bin: "110010"

                },

                {

                    b64: "z",

                    bin: "110011"

                },

                {

                    b64: "0",

                    bin: "110100"

                },

                {

                    b64: "1",

                    bin: "110101"

                },

                {

                    b64: "2",

                    bin: "110110"

                },

                {

                    b64: "3",

                    bin: "110111"

                },

                {

                    b64: "4",

                    bin: "111000"

                },

                {

                    b64: "5",

                    bin: "111001"

                },

                {

                    b64: "6",

                    bin: "111010"

                },

                {

                    b64: "7",

                    bin: "111011"

                },

                {

                    b64: "8",

                    bin: "111100"

                },

                {

                    b64: "9",

                    bin: "111101"

                },

                {

                    b64: "+",

                    bin: "111110"

                },

                {

                    b64: "/",

                    bin: "111111"

                }

            )

        },

        With(

            {

                BinRep: Concat(

                    AddColumns(

                        ForAll(

                            Split(

                                InputText,

                                ""

                            ),

                            {Result: ThisRecord.Value}

                        ),

                        "dec",

                        LookUp(

                            AsciiTable,

                            char = Result

                        ).Value

                    ),//Convert text to Ascii character code (decimal)

                    Concat(

                        Sequence(

                            8,

                            8,

                            -1

                        ),

                        Text(

                            If(

                                And(

                                    Mod(

                                        dec,

                                        Power(

                                            2,

                                            Value

                                        )

                                    ) >= Power(

                                        2,

                                        Value - 1

                                    ),

                                    Mod(

                                        dec,

                                        Power(

                                            2,

                                            Value

                                        )

                                    ) < Power(

                                        2,

                                        Value

                                    )

                                ),

                                1,

                                0

                            )

                        )

                    ) & "",

                    ""

                )//Convert decimal to binary

            },

            With(

                {

                    b64string: Concat(

                        Sequence(

                            RoundUp(

                                Len(BinRep) / 6,

                                0

                            ),

                            0

                        ),

                        LookUp(

                            B64ToBin,

                            bin = Mid(

                                BinRep & Left(

                                    "000000",

                                    6 - Mod(

                                        Len(BinRep),

                                        6

                                    )

                                ),

                                6 * Value + 1,

                                6

                            )//Left("000000"....) is padding right with zero

                        ).b64 & "",

                        ""

                    )

                },

                b64string & Left(

                    "====",

                    Mod(

                        4 - Mod(

                            Len(b64string),

                            4

                        ),

                        4

                    )

                )//Convert binary to base64

            )

        )

    )

);

Set(

    _searchResults,

    Office365Groups.HttpRequest(

        _appSearchEndPoint,

        "POST",

        _searchText

    )

);

Set(_searchResultUnTyped, Table(Index(Table(Index(Table(_searchResults.value),1).Value.hitsContainers),1).Value.hits));


ClearCollect(_searchResultCollection, ForAll( _searchResultUnTyped,

{ ID : Value(ThisRecord.Value.resource.fields.listItemId),

  Title : Text(ThisRecord.Value.resource.fields.title)

}));

//Clear the collection first

Clear(CollectionWithSequenceNumbers);

//For each item in the original collection, add a sequence number

ForAll(_searchResultCollection,

    Collect(CollectionWithSequenceNumbers,

        Last(FirstN(AddColumns(_searchResultCollection,"SequenceNumber",CountRows(CollectionWithSequenceNumbers)+1),CountRows(CollectionWithSequenceNumbers)+1)))

);

//ClearCollect(_searchResultCollectionWithSequence,Filter(CollectionWithSequenceNumbers,Value(ID) >= Value(var_pageNumber) And Value(ID) <= Value(var_pageNumber)*Value(var_pageSize)));


ClearCollect(_searchResultCollectionWithSequence,If(Value(var_pageNumber) = 1,FirstN(_searchResultCollection, var_pageSize),FirstN(LastN(_searchResultCollection,CountRows(_searchResultCollection) - Value(var_pageNumber)*Value(var_pageSize)),var_pageSize)));



If(Button3_16.Text = "Posts",ForAll(_searchResultCollectionWithSequence As SR,

Collect(col_allSearchresultsPosts, 

Filter(Posts, PostID = SR.Title))

),ForAll(_searchResultCollectionWithSequence As SR,

Collect(col_allSearchresultsPeople, 

Filter(Employees, Title = SR.Title))

));





/*ClearCollect(_searchResultCollection, ForAll( Table( ParseJSON( _searchResults ) ), { ChangedBy: Text(ThisRecord.Value.ChangedBy), Comments: Text(ThisRecord.Value.Comments), DateOfChange: DateTimeValue(ThisRecord.Value.DateOfChange), Status : Text(ThisRecord.Value.Status) } ))*/



Set(

    _showLoading,

    false

);


Add Service Principle support in Custom Connector

  then search for "Streamlining Integration: Using Service Principal authentication on Custom connectors with Microsoft Graph Applicati...