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

);


No comments:

Post a Comment

Add Service Principle support in Custom Connector

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