Update SharePoint list's Author / Editor field in App-Only context

Published Aug 09 2021 08:45 AM 1,438 Views
Occasional Contributor

Use Cases

 

  • You're migrating list items from a tenant to another using back-office app (console app or Azure SaaS resource) and want to keep existing fields, including author / editor ones
  • You're developing an API / Web App that will be used by employees which data is saved to a SharePoint List without  being in user context (because you want to make some background checks before saving)

 

When working with SharePoint in App-Only context, every update made on list items are done with the user "SharePoint App". But you can replace this by any user of the company!

 

One important thing to know is that the Azure AD Application that will be used must be granted to "Sites.FullControl.All", in order to update the Author / Editor field. Lower permissions won't let you update them.

 

Summary

 

In this article, we'll cover the following steps:

 

  • Setup the AAD Application
    • Generate self-signed certificate
    • Granting SharePoint app permission
  • Connect to SharePoint in App-Only context
  • Update list item Author / Editor fields with any other user than "SharePoint App"

 

 

Setup the AAD Application

 

The first thing to do is to register an application in Azure AD. As we'll use the client credential flow with a certificate, we first need to create a certificate.

 

Setup using PnP PowerShell

 

With PnP PowerShell, a simple command is enough to both declare self-signed certificate and register AAD Application with permissions using this certificate:

 

# Beware to mention an existing path in the -OutPath parameter, otherwise you won't get your certificate available
Register-PnPAzureADApp -ApplicationName "PnP.SharePoint.AppOnly" -Tenant contoso.onmicrosoft.com -OutPath c:\temp -CertificatePassword (ConvertTo-SecureString -String "password" -AsPlainText -Force) -SharePointApplicationPermissions "Sites.FullControl.All" -Store CurrentUser -DeviceLogin

 

This command does the following:

 

  • Generate a self-signed certificate with a password and stores it in the current user Local Certificate Store (Personal, this parameter works only on Windows)
  • Register an AAD Application with a certificate as an authentication flow, and a SharePoint App-Only permission

 

You can find more info about the command here.

 

Setup using CLI for Microsoft 365

 

For now there's no option to: 

 

  • generate both a self-signed certificate and register an AAD Application using it, in a single command. Requested here
  • admin consent permissions when registering an AAD Application. Requested here

 

Generate the self-signed certificate

 

That said, we have two possibilities depending on the environment, to genereate the self-signed certificate.

 

With PnP PowerShell command New-PnPAzureCertificate:
 
New-PnPAzureCertificate -OutCert "PnP.SharePoint.AppOnly.cer" -OutPfx "PnP.SharePoint.AppOnly.pfx" -ValidYears 1  -CertificatePassword (ConvertTo-SecureString -String "password" -AsPlainText -Force) -CommonName "PnP.SharePoint.AppOnly"

 

 

With OpenSSL:
 
# Replace the -nodes argument by -noenc if you're using OpenSSL 3.0, to disable encryption, since it will be done right after with the pfx file generation
openssl req -new -x509 -sha256 -newkey rsa:2048 -nodes -keyout PnP.SharePoint.AppOnly.key.pem -days 365 -out PnP.SharePoint.AppOnly.cert.pem -subj "/CN=contoso.com"


openssl pkcs12 -export -name "contoso.com" -out PnP.SharePoint.AppOnly.pfx -inkey PnP.SharePoint.AppOnly.key.pem -in PnP.SharePoint.AppOnly.cert.pem -password pass:password

 

 

The first command generates both certificate request and X.509 self-signed one (with private key RSA 2048 bits) which expires in 1 year. The ".cer.pem" exported file will be used for Azure AD Application registration.

 

The second command will generate the PFX file with a password, from both the private key input file (".key.pem") and the certificate request (".cert.pem") one. This one will be used for authentication, we'll see about that later.

 

Register the AAD Application

 

Once the certificate generated, we'll setup the app registration with the following command:

 

m365 aad app add --name 'PnP.SharePoint.AppOnly' --apisApplication 'https://microsoft.sharepoint-df.com/Sites.FullControl.All'

 

Wait a couple of minutes before being enabled to admin consent the permission through the Azure Portal.

 

Once done, stay on the AAD Application page and go to Certificates & Secrets and click on "Upload certificate" to add the "PnP.SharePoint.AppOnly.cert.pem" file.


aad-certificate.png

 

Authenticate to SharePoint & Update list item

 

Once everything's set for App-Only context, let's authenticate to SharePoint and update our list item!
 

Update with PnP Framework

 

The simpliest way is to use the PFX file created before, to get a context.

 

Let's try with a simple Console App:

 

 

static void Main(string[] args)
{
    string aadAppId = "[AAD_APP_ID]"; // Get Application ID / Client ID from the registration made before
    string pfxFilePath = "[PFX_FILE_PATH]"; // Like this: C:\\temp\\PnP.SharePoint.AppOnly.pfx
    string pfxPassword = "[PFX_PASSWORD]"; // For this example, it's "password"
    string tenant = "contoso.onmicrosoft.com";
    string spWeb = "htttps://contoso.sharepoint.com";


    var authManager = new AuthenticationManager(aadAppId, pfxFilePath, pfxPassword, tenant);


    using (var ctx = authManager.GetContext(spWeb))
    {
        Guid listId = new Guid("[LIST_ID]");
        int listItemId = 1;


        List lst = ctx.Web.GetListById(listId);
        ListItem itm = lst.GetItemById(listItemId);


        // Get Megan Bowen account
        User usr = ctx.Web.EnsureUser($"meganb@{tenant}");


        // Replace "Editor" by "Author" if you want
        itm["Editor"] = usr;
        itm.Update();


        ctx.ExecuteQuery();
    }
}

 

 

Update with PNP PowerShell

 
$aadAppId = "[AAD_APP_ID]" # Get Application ID / Client ID from the registration made before
$pfxFilePath = "[PFX_FILE_PATH]" # Like this: C:\\temp\\PnP.SharePoint.AppOnly.pfx
$pfxPassword = (ConvertTo-SecureString -String "[PFX_PASSWORD]" -AsPlainText -Force) # For this example, it's "password"
$tenant = "contoso.onmicrosoft.com"
$spWeb = "https://contoso.sharepoint.com"


Connect-PnPOnline -Url $spWeb -Tenant $tenant -ClientId $aadAppId -CertificatePath $pfxFilePath -CertificatePassword $pfxPassword


$listId = "53006cd2-871c-4683-aaf9-f67affa2de5b"
$lisItemId = 1


# Replace "Editor" by "Author" if you want
Set-PnPListItem -List $listId -Identity $lisItemId -Values @{"Editor"="meganb@" + $tenant}

 

Update with CLI for Microsoft 365

 

aadAppId="[AAD_APP_ID]" # Get Application ID / Client ID from the registration made before
pfxFilePath="[PFX_FILE_PATH]" # Like this: /home/PnP.SharePoint.AppOnly.pfx
pfxPassword="[PFX_PASSWORD]" # For this example, it's "password"
tenant="contoso.onmicrosoft.com"
spWeb="https://contoso.sharepoint.com"


m365 login --authType certificate --certificateFile $pfxFilePath --password $pfxPassword --appId $aadAppId --tenant $tenant


listId="53006cd2-871c-4683-aaf9-f67affa2de5b"
lisItemId=1


# Replace "--Editor" by "--Author" if you want
m365 spo listitem set --listId $listId --id $lisItemId --webUrl $spWeb --Editor "[{'Key':'i:0#.f|membership|meganb@${tenant}'}]"

 

And that's it! You should now be able to update those specific fields in any way :stareyes:

 

Happy coding!
%3CLINGO-SUB%20id%3D%22lingo-sub-2622569%22%20slang%3D%22en-US%22%3EUpdate%20SharePoint%20list's%20Author%20%2F%20Editor%20field%20in%20App-Only%20context%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2622569%22%20slang%3D%22en-US%22%3E%3CH2%20id%3D%22toc-hId--383752434%22%20id%3D%22toc-hId--383752434%22%3E%3CSPAN%3EUse%26nbsp%3BCases%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%3EYou're%26nbsp%3Bmigrating%26nbsp%3Blist%26nbsp%3Bitems%26nbsp%3Bfrom%26nbsp%3Ba%26nbsp%3Btenant%26nbsp%3Bto%26nbsp%3Banother%26nbsp%3Busing%20back-office%20app%20(console%20app%20or%20Azure%20SaaS%20resource)%20and%20want%20to%20keep%20existing%20fields%2C%20including%20author%20%2F%20editor%20ones%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3EYou're%20developing%20an%20API%20%2F%26nbsp%3BWeb%26nbsp%3BApp%26nbsp%3Bthat%26nbsp%3Bwill%26nbsp%3Bbe%26nbsp%3Bused%26nbsp%3Bby%26nbsp%3Bemployees%26nbsp%3Bwhich%26nbsp%3Bdata%26nbsp%3Bis%26nbsp%3Bsaved%26nbsp%3Bto%26nbsp%3Ba%26nbsp%3BSharePoint%26nbsp%3BList%26nbsp%3Bwithout%26nbsp%3B%20being%20in%20user%20context%20(because%20you%20want%20to%20make%20some%20background%20checks%20before%20saving)%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20working%20with%20SharePoint%20in%20App-Only%20context%3CSPAN%3E%2C%26nbsp%3Bevery%26nbsp%3Bupdate%26nbsp%3Bmade%26nbsp%3Bon%26nbsp%3Blist%26nbsp%3Bitems%26nbsp%3Bare%26nbsp%3Bdone%26nbsp%3Bwith%26nbsp%3Bthe%26nbsp%3Buser%26nbsp%3B%22SharePoint%26nbsp%3BApp%22.%20But%20you%20can%20replace%20this%20by%20any%20user%20of%20the%20company!%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CU%3EOne%26nbsp%3Bimportant%26nbsp%3Bthing%26nbsp%3Bto%26nbsp%3Bknow%26nbsp%3Bis%26nbsp%3Bthat%26nbsp%3Bthe%26nbsp%3BAzure%26nbsp%3BAD%26nbsp%3BApplication%26nbsp%3Bthat%26nbsp%3Bwill%26nbsp%3Bbe%26nbsp%3Bused%26nbsp%3Bmust%26nbsp%3Bbe%26nbsp%3Bgranted%26nbsp%3Bto%26nbsp%3B%22Sites.FullControl.All%22%2C%26nbsp%3Bin%26nbsp%3Border%26nbsp%3Bto%26nbsp%3Bupdate%26nbsp%3Bthe%26nbsp%3BAuthor%26nbsp%3B%2F%26nbsp%3BEditor%26nbsp%3Bfield.%26nbsp%3BLower%26nbsp%3Bpermissions%26nbsp%3Bwon't%26nbsp%3Blet%26nbsp%3Byou%26nbsp%3Bupdate%26nbsp%3Bthem.%3C%2FU%3E%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-2103760399%22%20id%3D%22toc-hId-2103760399%22%3E%3CSPAN%3ESummary%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CSPAN%3EIn%26nbsp%3Bthis%26nbsp%3Barticle%2C%26nbsp%3Bwe'll%26nbsp%3Bcover%26nbsp%3Bthe%26nbsp%3Bfollowing%26nbsp%3Bsteps%3A%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%3ESetup%26nbsp%3Bthe%26nbsp%3BAAD%26nbsp%3BApplication%3C%2FSPAN%3E%3CUL%3E%0A%3CLI%3EGenerate%26nbsp%3Bself-signed%26nbsp%3Bcertificate%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3EGranting%26nbsp%3BSharePoint%26nbsp%3Bapp%26nbsp%3Bpermission%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3CLI%3EConnect%26nbsp%3Bto%26nbsp%3BSharePoint%26nbsp%3Bin%26nbsp%3BApp-Only%26nbsp%3Bcontext%3C%2FLI%3E%0A%3CLI%3EUpdate%26nbsp%3Blist%26nbsp%3Bitem%20Author%20%2F%20Editor%20fields%20with%20any%20other%20user%20than%20%22SharePoint%20App%22%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CSPAN%3EWe'll%26nbsp%3Buse%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fpowershell%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3E%3CSPAN%3EPnP%26nbsp%3BPowerShell%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%2C%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fcli-microsoft365%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3E%3CSPAN%3ECLI%26nbsp%3Bfor%26nbsp%3BMicrosoft%26nbsp%3B365%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%26nbsp%3Band%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fpnp%2Fpnpframework%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%3CSPAN%3EPnP%26nbsp%3BFramework%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E!%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-296305936%22%20id%3D%22toc-hId-296305936%22%3E%3CSPAN%3ESetup%26nbsp%3Bthe%26nbsp%3BAAD%26nbsp%3BApplication%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CSPAN%3EThe%26nbsp%3Bfirst%26nbsp%3Bthing%26nbsp%3Bto%26nbsp%3Bdo%26nbsp%3Bis%26nbsp%3Bto%26nbsp%3Bregister%26nbsp%3Ban%26nbsp%3Bapplication%26nbsp%3Bin%26nbsp%3BAzure%26nbsp%3BAD.%26nbsp%3BAs%26nbsp%3Bwe'll%26nbsp%3Buse%26nbsp%3Bthe%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fdevelop%2Fv2-oauth2-client-creds-grant-flow%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%3CSPAN%3Eclient%26nbsp%3Bcredential%26nbsp%3Bflow%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%26nbsp%3Bwith%26nbsp%3Ba%26nbsp%3Bcertificate%2C%26nbsp%3Bwe%26nbsp%3Bfirst%26nbsp%3Bneed%26nbsp%3Bto%26nbsp%3Bcreate%26nbsp%3Ba%26nbsp%3Bcertificate.%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-986867410%22%20id%3D%22toc-hId-986867410%22%3E%3CSPAN%3ESetup%26nbsp%3Busing%26nbsp%3BPnP%26nbsp%3BPowerShell%3C%2FSPAN%3E%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EWith%26nbsp%3BPnP%26nbsp%3BPowerShell%2C%26nbsp%3Ba%26nbsp%3Bsimple%26nbsp%3Bcommand%26nbsp%3Bis%26nbsp%3Benough%26nbsp%3Bto%26nbsp%3Bboth%26nbsp%3Bdeclare%20self-signed%20certificate%20and%20register%20AAD%20Application%20with%20permissions%20using%20this%20certificate%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%20Beware%20to%20mention%20an%20existing%20path%20in%20the%20-OutPath%20parameter%2C%20otherwise%20you%20won't%20get%20your%20certificate%20available%0ARegister-PnPAzureADApp%20-ApplicationName%20%22PnP.SharePoint.AppOnly%22%20-Tenant%20contoso.onmicrosoft.com%20-OutPath%20c%3A%5Ctemp%20-CertificatePassword%20(ConvertTo-SecureString%20-String%20%22password%22%20-AsPlainText%20-Force)%20-SharePointApplicationPermissions%20%22Sites.FullControl.All%22%20-Store%20CurrentUser%20-DeviceLogin%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CSPAN%3EThis%26nbsp%3Bcommand%26nbsp%3Bdoes%26nbsp%3Bthe%26nbsp%3Bfollowing%3A%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%3EGenerate%20a%20self-signed%20certificate%20with%26nbsp%3Ba%26nbsp%3Bpassword%26nbsp%3Band%26nbsp%3Bstores%26nbsp%3Bit%26nbsp%3Bin%26nbsp%3Bthe%26nbsp%3Bcurrent%26nbsp%3Buser%26nbsp%3BLocal%26nbsp%3BCertificate%26nbsp%3BStore%20(Personal%2C%20this%20parameter%20works%20only%20on%20Windows)%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3ERegister%26nbsp%3Ban%26nbsp%3BAAD%26nbsp%3BApplication%26nbsp%3Bwith%20a%20certificate%26nbsp%3Bas%26nbsp%3Ban%26nbsp%3Bauthentication%26nbsp%3Bflow%2C%26nbsp%3Band%26nbsp%3Ba%26nbsp%3BSharePoint%26nbsp%3BApp-Only%26nbsp%3Bpermission%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CSPAN%3EYou%26nbsp%3Bcan%26nbsp%3Bfind%26nbsp%3Bmore%26nbsp%3Binfo%26nbsp%3Babout%26nbsp%3Bthe%26nbsp%3Bcommand%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fpowershell%2Fcmdlets%2FRegister-PnPAzureADApp.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehere%3C%2FA%3E.%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--820587053%22%20id%3D%22toc-hId--820587053%22%3E%3CSPAN%3ESetup%26nbsp%3Busing%26nbsp%3BCLI%26nbsp%3Bfor%26nbsp%3BMicrosoft%26nbsp%3B365%3C%2FSPAN%3E%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CSPAN%3EFor%26nbsp%3Bnow%26nbsp%3Bthere's%26nbsp%3Bno%26nbsp%3Boption%26nbsp%3Bto%3A%26nbsp%3B%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%3Egenerate%26nbsp%3Bboth%26nbsp%3Ba%26nbsp%3Bself-signed%26nbsp%3Bcertificate%26nbsp%3Band%26nbsp%3Bregister%26nbsp%3Ban%26nbsp%3BAAD%26nbsp%3BApplication%26nbsp%3Busing%26nbsp%3Bit%2C%26nbsp%3Bin%26nbsp%3Ba%26nbsp%3Bsingle%26nbsp%3Bcommand.%26nbsp%3BRequested%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fpnp%2Fcli-microsoft365%2Fissues%2F2170%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%3CSPAN%3Ehere%3C%2FSPAN%3E%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3Eadmin%26nbsp%3Bconsent%26nbsp%3Bpermissions%26nbsp%3Bwhen%26nbsp%3Bregistering%26nbsp%3Ban%26nbsp%3BAAD%26nbsp%3BApplication.%26nbsp%3BRequested%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fpnp%2Fcli-microsoft365%2Fissues%2F2563%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3Ehere%3C%2FSPAN%3E%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId--130025579%22%20id%3D%22toc-hId--130025579%22%3E%3CSPAN%3EGenerate%26nbsp%3Bthe%26nbsp%3Bself-signed%26nbsp%3Bcertificate%3C%2FSPAN%3E%3C%2FH4%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CSPAN%3EThat%26nbsp%3Bsaid%2C%26nbsp%3Bwe%26nbsp%3Bhave%26nbsp%3Btwo%26nbsp%3Bpossibilities%26nbsp%3Bdepending%26nbsp%3Bon%26nbsp%3Bthe%26nbsp%3Benvironment%2C%26nbsp%3Bto%26nbsp%3Bgenereate%26nbsp%3Bthe%26nbsp%3Bself-signed%26nbsp%3Bcertificate.%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CSPAN%3EWith%26nbsp%3BPnP%26nbsp%3BPowerShell%26nbsp%3Bcommand%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fpowershell%2Fcmdlets%2FNew-PnPAzureCertificate.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3E%3CSPAN%3ENew-PnPAzureCertificate%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3ENew-PnPAzureCertificate%20-OutCert%20%22PnP.SharePoint.AppOnly.cer%22%20-OutPfx%20%22PnP.SharePoint.AppOnly.pfx%22%20-ValidYears%201%20%20-CertificatePassword%20(ConvertTo-SecureString%20-String%20%22password%22%20-AsPlainText%20-Force)%20-CommonName%20%22PnP.SharePoint.AppOnly%22%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CSPAN%3EWith%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.openssl.org%2Fdocs%2Fmanmaster%2Fman1%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3E%3CSPAN%3EOpenSSL%3A%3C%2FSPAN%3E%3C%2FA%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3E%23%20Replace%20the%20-nodes%20argument%20by%20-noenc%20if%20you're%20using%20OpenSSL%203.0%2C%20to%20disable%20encryption%2C%20since%20it%20will%20be%20done%20right%20after%20with%20the%20pfx%20file%20generation%0Aopenssl%20req%20-new%20-x509%20-sha256%20-newkey%20rsa%3A2048%20-nodes%20-keyout%20PnP.SharePoint.AppOnly.key.pem%20-days%20365%20-out%20PnP.SharePoint.AppOnly.cert.pem%20-subj%20%22%2FCN%3Dcontoso.com%22%0A%0A%0Aopenssl%20pkcs12%20-export%20-name%20%22contoso.com%22%20-out%20PnP.SharePoint.AppOnly.pfx%20-inkey%20PnP.SharePoint.AppOnly.key.pem%20-in%20PnP.SharePoint.AppOnly.cert.pem%20-password%20pass%3Apassword%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20first%20command%20generates%20both%20certificate%20request%20and%20X.509%20self-signed%20one%26nbsp%3B%3CSPAN%3E(with%26nbsp%3Bprivate%26nbsp%3Bkey%26nbsp%3BRSA%26nbsp%3B2048%26nbsp%3Bbits)%20which%20expires%20in%201%20year.%20The%26nbsp%3B%22.cer.pem%22%26nbsp%3Bexported%26nbsp%3Bfile%26nbsp%3Bwill%26nbsp%3Bbe%26nbsp%3Bused%26nbsp%3Bfor%26nbsp%3BAzure%26nbsp%3BAD%26nbsp%3BApplication%26nbsp%3Bregistration.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%26nbsp%3Bsecond%26nbsp%3Bcommand%26nbsp%3Bwill%26nbsp%3Bgenerate%26nbsp%3Bthe%26nbsp%3BPFX%26nbsp%3Bfile%26nbsp%3Bwith%26nbsp%3Ba%26nbsp%3Bpassword%2C%26nbsp%3Bfrom%26nbsp%3Bboth%26nbsp%3Bthe%26nbsp%3Bprivate%26nbsp%3Bkey%26nbsp%3Binput%26nbsp%3Bfile%26nbsp%3B(%22.key.pem%22)%26nbsp%3Band%26nbsp%3Bthe%26nbsp%3Bcertificate%26nbsp%3Brequest%26nbsp%3B(%22.cert.pem%22)%26nbsp%3Bone.%26nbsp%3BThis%26nbsp%3Bone%26nbsp%3Bwill%26nbsp%3Bbe%26nbsp%3Bused%26nbsp%3Bfor%26nbsp%3Bauthentication%2C%20we'll%26nbsp%3Bsee%26nbsp%3Babout%26nbsp%3Bthat%26nbsp%3Blater.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId--1937480042%22%20id%3D%22toc-hId--1937480042%22%3E%3CSPAN%3ERegister%26nbsp%3Bthe%26nbsp%3BAAD%26nbsp%3BApplication%3C%2FSPAN%3E%3C%2FH4%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CSPAN%3EOnce%26nbsp%3Bthe%26nbsp%3Bcertificate%26nbsp%3Bgenerated%2C%26nbsp%3Bwe'll%26nbsp%3Bsetup%26nbsp%3Bthe%26nbsp%3Bapp%26nbsp%3Bregistration%26nbsp%3Bwith%26nbsp%3Bthe%26nbsp%3Bfollowing%26nbsp%3Bcommand%3A%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Em365%20aad%20app%20add%20--name%20'PnP.SharePoint.AppOnly'%20--apisApplication%20'https%3A%2F%2Fmicrosoft.sharepoint-df.com%2FSites.FullControl.All'%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CSPAN%3EWait%26nbsp%3Ba%26nbsp%3Bcouple%26nbsp%3Bof%26nbsp%3Bminutes%26nbsp%3Bbefore%26nbsp%3Bbeing%26nbsp%3Benabled%26nbsp%3Bto%26nbsp%3Badmin%26nbsp%3Bconsent%26nbsp%3Bthe%26nbsp%3Bpermission%26nbsp%3Bthrough%26nbsp%3Bthe%26nbsp%3BAzure%26nbsp%3BPortal.%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CSPAN%3EOnce%26nbsp%3Bdone%2C%26nbsp%3Bstay%26nbsp%3Bon%26nbsp%3Bthe%26nbsp%3BAAD%26nbsp%3BApplication%26nbsp%3Bpage%26nbsp%3Band%26nbsp%3Bgo%26nbsp%3Bto%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3ECertificates%26nbsp%3B%26amp%3B%26nbsp%3BSecrets%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3Band%26nbsp%3Bclick%26nbsp%3Bon%26nbsp%3B%22%3C%2FSPAN%3E%3CSPAN%3E%3CEM%3EUpload%26nbsp%3Bcertificate%3C%2FEM%3E%3C%2FSPAN%3E%3CSPAN%3E%22%26nbsp%3Bto%26nbsp%3Badd%26nbsp%3Bthe%26nbsp%3B%22%3C%2FSPAN%3E%3CEM%3EPnP.SharePoint.AppOnly.cert.pem%3C%2FEM%3E%3CSPAN%3E%22%26nbsp%3Bfile.%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CP%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22aad-certificate.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F301355i0B218B7BCDBBD7F6%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22aad-certificate.png%22%20alt%3D%22aad-certificate.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--151031787%22%20id%3D%22toc-hId--151031787%22%3E%3CSPAN%3EAuthenticate%26nbsp%3Bto%26nbsp%3BSharePoint%26nbsp%3B%26amp%3B%26nbsp%3BUpdate%26nbsp%3Blist%26nbsp%3Bitem%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CSPAN%3EOnce%26nbsp%3Beverything's%26nbsp%3Bset%26nbsp%3Bfor%26nbsp%3BApp-Only%26nbsp%3Bcontext%2C%26nbsp%3Blet's%26nbsp%3Bauthenticate%26nbsp%3Bto%26nbsp%3BSharePoint%26nbsp%3Band%26nbsp%3Bupdate%26nbsp%3Bour%26nbsp%3Blist%26nbsp%3Bitem!%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CH3%20id%3D%22toc-hId-539529687%22%20id%3D%22toc-hId-539529687%22%3E%3CSPAN%3EUpdate%26nbsp%3Bwith%26nbsp%3BPnP%26nbsp%3BFramework%3C%2FSPAN%3E%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CSPAN%3EThe%26nbsp%3Bsimpliest%26nbsp%3Bway%26nbsp%3Bis%26nbsp%3Bto%26nbsp%3Buse%26nbsp%3Bthe%26nbsp%3BPFX%26nbsp%3Bfile%26nbsp%3Bcreated%26nbsp%3Bbefore%2C%26nbsp%3Bto%26nbsp%3Bget%26nbsp%3Ba%26nbsp%3Bcontext.%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CSPAN%3ELet's%26nbsp%3Btry%26nbsp%3Bwith%26nbsp%3Ba%26nbsp%3Bsimple%26nbsp%3BConsole%26nbsp%3BApp%3A%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Estatic%20void%20Main(string%5B%5D%20args)%0A%7B%0A%20%20%20%20string%20aadAppId%20%3D%20%22%5BAAD_APP_ID%5D%22%3B%20%2F%2F%20Get%20Application%20ID%20%2F%20Client%20ID%20from%20the%20registration%20made%20before%0A%20%20%20%20string%20pfxFilePath%20%3D%20%22%5BPFX_FILE_PATH%5D%22%3B%20%2F%2F%20Like%20this%3A%20C%3A%5C%5Ctemp%5C%5CPnP.SharePoint.AppOnly.pfx%0A%20%20%20%20string%20pfxPassword%20%3D%20%22%5BPFX_PASSWORD%5D%22%3B%20%2F%2F%20For%20this%20example%2C%20it's%20%22password%22%0A%20%20%20%20string%20tenant%20%3D%20%22contoso.onmicrosoft.com%22%3B%0A%20%20%20%20string%20spWeb%20%3D%20%22htttps%3A%2F%2Fcontoso.sharepoint.com%22%3B%0A%0A%0A%20%20%20%20var%20authManager%20%3D%20new%20AuthenticationManager(aadAppId%2C%20pfxFilePath%2C%20pfxPassword%2C%20tenant)%3B%0A%0A%0A%20%20%20%20using%20(var%20ctx%20%3D%20authManager.GetContext(spWeb))%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Guid%20listId%20%3D%20new%20Guid(%22%5BLIST_ID%5D%22)%3B%0A%20%20%20%20%20%20%20%20int%20listItemId%20%3D%201%3B%0A%0A%0A%20%20%20%20%20%20%20%20List%20lst%20%3D%20ctx.Web.GetListById(listId)%3B%0A%20%20%20%20%20%20%20%20ListItem%20itm%20%3D%20lst.GetItemById(listItemId)%3B%0A%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20Get%20Megan%20Bowen%20account%0A%20%20%20%20%20%20%20%20User%20usr%20%3D%20ctx.Web.EnsureUser(%24%22meganb%40%7Btenant%7D%22)%3B%0A%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20Replace%20%22Editor%22%20by%20%22Author%22%20if%20you%20want%0A%20%20%20%20%20%20%20%20itm%5B%22Editor%22%5D%20%3D%20usr%3B%0A%20%20%20%20%20%20%20%20itm.Update()%3B%0A%0A%0A%20%20%20%20%20%20%20%20ctx.ExecuteQuery()%3B%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1267924776%22%20id%3D%22toc-hId--1267924776%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22toc-hId-524424578%22%20id%3D%22toc-hId-524424578%22%3E%3CSPAN%3EUpdate%26nbsp%3Bwith%26nbsp%3BPNP%26nbsp%3BPowerShell%3C%2FSPAN%3E%3C%2FH3%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24aadAppId%20%3D%20%22%5BAAD_APP_ID%5D%22%20%23%20Get%20Application%20ID%20%2F%20Client%20ID%20from%20the%20registration%20made%20before%0A%24pfxFilePath%20%3D%20%22%5BPFX_FILE_PATH%5D%22%20%23%20Like%20this%3A%20C%3A%5C%5Ctemp%5C%5CPnP.SharePoint.AppOnly.pfx%0A%24pfxPassword%20%3D%20(ConvertTo-SecureString%20-String%20%22%5BPFX_PASSWORD%5D%22%20-AsPlainText%20-Force)%20%23%20For%20this%20example%2C%20it's%20%22password%22%0A%24tenant%20%3D%20%22contoso.onmicrosoft.com%22%0A%24spWeb%20%3D%20%22https%3A%2F%2Fcontoso.sharepoint.com%22%0A%0A%0AConnect-PnPOnline%20-Url%20%24spWeb%20-Tenant%20%24tenant%20-ClientId%20%24aadAppId%20-CertificatePath%20%24pfxFilePath%20-CertificatePassword%20%24pfxPassword%0A%0A%0A%24listId%20%3D%20%2253006cd2-871c-4683-aaf9-f67affa2de5b%22%0A%24lisItemId%20%3D%201%0A%0A%0A%23%20Replace%20%22Editor%22%20by%20%22Author%22%20if%20you%20want%0ASet-PnPListItem%20-List%20%24listId%20-Identity%20%24lisItemId%20-Values%20%40%7B%22Editor%22%3D%22meganb%40%22%20%2B%20%24tenant%7D%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3CH3%20id%3D%22toc-hId--1283029885%22%20id%3D%22toc-hId--1283029885%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22toc-hId-1204482948%22%20id%3D%22toc-hId-1204482948%22%3E%3CSPAN%3EUpdate%26nbsp%3Bwith%26nbsp%3BCLI%26nbsp%3Bfor%26nbsp%3BMicrosoft%26nbsp%3B365%3C%2FSPAN%3E%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3EaadAppId%3D%22%5BAAD_APP_ID%5D%22%20%23%20Get%20Application%20ID%20%2F%20Client%20ID%20from%20the%20registration%20made%20before%0ApfxFilePath%3D%22%5BPFX_FILE_PATH%5D%22%20%23%20Like%20this%3A%20%2Fhome%2FPnP.SharePoint.AppOnly.pfx%0ApfxPassword%3D%22%5BPFX_PASSWORD%5D%22%20%23%20For%20this%20example%2C%20it's%20%22password%22%0Atenant%3D%22contoso.onmicrosoft.com%22%0AspWeb%3D%22https%3A%2F%2Fcontoso.sharepoint.com%22%0A%0A%0Am365%20login%20--authType%20certificate%20--certificateFile%20%24pfxFilePath%20--password%20%24pfxPassword%20--appId%20%24aadAppId%20--tenant%20%24tenant%0A%0A%0AlistId%3D%2253006cd2-871c-4683-aaf9-f67affa2de5b%22%0AlisItemId%3D1%0A%0A%0A%23%20Replace%20%22--Editor%22%20by%20%22--Author%22%20if%20you%20want%0Am365%20spo%20listitem%20set%20--listId%20%24listId%20--id%20%24lisItemId%20--webUrl%20%24spWeb%20--Editor%20%22%5B%7B'Key'%3A'i%3A0%23.f%7Cmembership%7Cmeganb%40%24%7Btenant%7D'%7D%5D%22%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CSPAN%3EAnd%26nbsp%3Bthat's%20it!%26nbsp%3BYou%26nbsp%3Bshould%26nbsp%3Bnow%26nbsp%3Bbe%26nbsp%3Bable%26nbsp%3Bto%26nbsp%3Bupdate%26nbsp%3Bthose%26nbsp%3Bspecific%26nbsp%3Bfields%26nbsp%3Bin%26nbsp%3Bany%26nbsp%3Bway%26nbsp%3B%3CIMG%20class%3D%22lia-deferred-image%20lia-image-emoji%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Fhtml%2F%40A027B0AAF3CA617A1E2E22C4E761B2FE%2Fimages%2Femoticons%2Fstareyes_40x40.gif%22%20alt%3D%22%3Astareyes%3A%22%20title%3D%22%3Astareyes%3A%22%20%2F%3E%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%3CSPAN%3EHappy%26nbsp%3Bcoding!%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2622569%22%20slang%3D%22en-US%22%3E%3CDIV%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22nantes-4078905_1280.jpg%22%20style%3D%22width%3A%20749px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F301354iD7A5A38F6474BF66%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22nantes-4078905_1280.jpg%22%20alt%3D%22nantes-4078905_1280.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3EWhen%20working%20with%20SharePoint%20in%20App-Only%20context%3CSPAN%3E%2C%26nbsp%3Bevery%26nbsp%3Bupdate%26nbsp%3Bmade%26nbsp%3Bon%26nbsp%3Blist%26nbsp%3Bitems%26nbsp%3Bare%26nbsp%3Bdone%26nbsp%3Bwith%26nbsp%3Bthe%26nbsp%3Buser%26nbsp%3B%22SharePoint%26nbsp%3BApp%22.%20But%20you%20can%20replace%20this%20by%20any%20user%20of%20the%20company!%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2622569%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ECLI%20for%20Microsoft%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPnP%20PowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Aug 11 2021 05:51 AM
Updated by: