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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

How can I upload/update a file to WebFiles in PowerApps Portal using PowerShell

(0) ShareShare
ReportReport
Posted on by 15

I would like to be able to manage files (images) and create files (minified css/js) on my local filesystem (and check into sourcecontrol), and have a PowerShell script to create/update/delete these files on my PowerApps Portal in the WebFiles.

I have been playing with PowerShell, and I'm able to update an annotation (the actual file record connected to an adx_webfile, but I have no clue how to create a new adx_webfile entry and the corresponding annotation for the file contents.

Some things I did to update the contents of the annotation belonging to an existing adx_webfile:

param(
[string]$organization = "myOrg",
[string]$username = "serge.van.den.oever@macaw.nl",
[string]$password = "myPassword",
[string]$filename = "theme.css",
[string]$content = "Make my day"

)

$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)

Install-Module -Name Microsoft.Xrm.Data.PowerShell
Import-Module -Name Microsoft.Xrm.Data.PowerShell

$conn = Get-CrmConnection -DeploymentRegion EMEA -OnLineType Office365 -Credential $mycreds -OrganizationName $organization

$base64encodedContent = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($content))

$fetchAnnotation = @"
<fetch>
<entity name="annotation" >
<filter>
<condition attribute="filename" operator="eq" value="$filename" />
</filter>
</entity>
</fetch>
"@

$records = Get-CrmRecordsByFetch -conn $conn -Fetch $fetchAnnotation
if ($records.Count -ne 1) {
write-error "Expected file '$filename' not found"
exit 1
}

$fileRecord = $records.CrmRecords[0]
$fileRecord.documentbody = $base64encodedContent
Set-CrmRecord -conn $conn -CrmRecord $fileRecord


But this only works on existing annotations that already belong to an axd_webfile.

I already used export tooling to investigate what fields are involved:

<entities xmlns:xsd="">www.w3.org/.../XMLSchema" xmlns:xsi="">www.w3.org/.../XMLSchema-instance" timestamp="2020-01-09T20:26:23.7143227Z">
<entity name="adx_webfile" displayname="Web File">
<records>
<record id="7c972589-66ee-4c11-be72-193570c03355">
<field name="adx_contentdisposition" value="756150000" />
<field name="adx_enabletracking" value="False" />
<field name="adx_excludefromsearch" value="True" />
<field name="adx_hiddenfromsitemap" value="False" />
<field name="adx_name" value="theme.css" />
<field name="adx_parentpageid" value="87cdfb86-e3c8-48d7-8a99-c6fb2cbcb845" lookupentity="adx_webpage" lookupentityname="Home" />
<field name="adx_partialurl" value="theme.css" />
<field name="adx_publishingstateid" value="38c7d71a-31fd-4c2a-b7b0-fa2cc7381654 lookupentity="adx_publishingstate" lookupentityname="Published" />
<field name="statecode" value="0" />
<field name="statuscode" value="1" />
<field name="adx_webfileid" value="7c972589-66ee-4c11-be72-193570c03355" />
<field name="adx_websiteid" value="f46b70cc-580b-4f1a-87c3-41deb48eb90d" lookupentity="adx_website" lookupentityname="Starter Portal" />
</record>
</records>
<entity name="annotation" displayname="Note">
<records>
<record id="eef1b8e5-a7f7-4efd-b0a1-f617db2dd52a">
<field name="documentbody" value="Base64" />
<field name="filename" value="theme.css" />
<field name="filesize" value="50798" />
<field name="isdocument" value="True" />
<field name="mimetype" value="text/css" />
<field name="annotationid" value="eef1b8e5-a7f7-4efd-b0a1-f617db2dd52a" />
<field name="objecttypecode" value="adx_webfile" />
<field name="objectid" value="7c972589-66ee-4c11-be72-193570c03355" lookupentity="adx_webfile" />
</record>
</records>
<m2mrelationships />
</entity>
<entities>

But don't know how to go from here. Any suggestions would be appreciated!

I have the same question (0)
  • Suggested answer
    oliver.rodrigues Profile Picture
    4,052 on at

    I think this relates to the previous post, do you really want to create your custom code to create the Portal records in CRM?

    Microsoft provides configuration migration tool for that, there are also tools from XrmToolbox, there is CI/CD framework from the community (Microsoft is also building their own), I sent the link for this in the other post

    Have you considered using any existing tool?

  • Suggested answer
    Eric Sutter Profile Picture
    5 on at

    I know this is years late though I was googling the same thing as we have a build process that requires pushing js bundles from react code to portals.
    So if anyone else is searching for a straight forward example of making the file here you go.

    You were close getting this working.
    Check out below I have a end to end example of creating a web file and the associated note.

    Install-Module -Name Microsoft.Xrm.Data.PowerShell
    Import-Module -Name Microsoft.Xrm.Data.PowerShell
    
    connect-crmonline -Username "****" -ServerUrl ***.crm3.dynamics.com
    
    $fileName = "test2.json"
    $content = '{"Field":"My Value"}'
    $base64encodedContent = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($content))
    
    # hard coded here but could be queried or passed in from devOps pipeline or command line
    $website = [Microsoft.Xrm.Sdk.EntityReference]::new("adx_website","d28688b5-****-****-****-000d3ac9b6f4")
    $parentPage = [Microsoft.Xrm.Sdk.EntityReference]::new("adx_webpage","5b3090af-****-****-****-000d3ac9b6f4")
    $publishingProfile = [Microsoft.Xrm.Sdk.EntityReference]::new("adx_publishingstate","013090af-****-****-****-000d3ac9b6f4")
    $contentDisposition = [Microsoft.Xrm.Sdk.OptionSetValue]::new(756150000)
    
    #Create webfile
    $webfileId = New-CrmRecord -EntityLogicalName adx_webfile -Fields @{"adx_name"=$fileName;"adx_partialurl"=$fileName;"adx_websiteid"=$website;"adx_parentpageid"=$parentPage;"adx_publishingstateid"=$publishingProfile;"adx_contentdisposition"=$contentDisposition;} 
    $webfile = Get-CrmRecord -EntityLogicalName adx_webfile -Id $webfileId -Fields *
    
     
    #Create annotation
    $webfileId = New-CrmRecord -EntityLogicalName annotation -Fields @{"filename"=$fileName;"isdocument"=$True;"objectid"=$webfile.EntityReference;"documentbody"=$base64encodedContent;} 
    
    # Now that you see the create easily can do same sort of logic to update based of finding an existing file and updating the content. 

    I like the simple straight forward nature of utilizing the Microsoft.Xrm.Data.PowerShell module over utilizing the CLI tools which would involve building packages and deploying. 
    This is built for task and can use these pieces to make reusable pipeline scripts where the Microsoft published tooling is not specific enough to the task you are trying to achieve. 

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans