How to set property in property bag on root site collection using JSOM?

%3CLINGO-SUB%20id%3D%22lingo-sub-1745591%22%20slang%3D%22en-US%22%3EHow%20to%20set%20property%20in%20property%20bag%20on%20root%20site%20collection%20using%20JSOM%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1745591%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20trying%20to%20update%2Fcreate%20property%20in%20PropertyBag.%20Its%20working%20fine%20on%20classic%20experience%20(developer%20site%20and%20document%20center)%20but%20its%20not%20allowing%20application%20to%20start%20on%20modern%20site%20collections(root%20site%2C%20communication%20site%20and%20team%20site)%20and%20its%20not%20setting%20property%20in%20PropertyBag.%3C%2FP%3E%3CP%3EWhen%20I%20try%20to%20run%20application%20root%20site%20collection%20after%20installation%20its%20throwing%20this%20error%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CPRE%3EAccess%20denied.%20You%20%3CSPAN%20class%3D%22hljs-keyword%22%3Edo%3C%2FSPAN%3E%20not%20have%20permission%20to%20perform%20%3CSPAN%20class%3D%22hljs-keyword%22%3Ethis%3C%2FSPAN%3E%20action%20or%20access%20%3CSPAN%20class%3D%22hljs-keyword%22%3Ethis%3C%2FSPAN%3E%20resource.%3C%2FPRE%3E%3CP%3E%3CSPAN%3EPlease%20tell%20me%20how%20can%20I%20set%20property%20in%20property%20bag%20for%20root%20site%2C%20communication%20site%20and%20team%20site.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20context%20%3D%20SP.ClientContext.get_current()%3B%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20web%20%3D%20LawApp.Repositories.getWeb(context%2C%20hostUrl)%3B%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20props%20%3D%20web.get_allProperties()%3B%0A%0A%20%20%20%20%20%20%20%20props.set_item(%3CSPAN%20class%3D%22hljs-string%22%3E%22CurrentVersion%22%3C%2FSPAN%3E%2C%20%3CSPAN%20class%3D%22hljs-number%22%3E2002%3C%2FSPAN%3E)%3B%0A%20%20%20%20%20%20%20%20web.update()%3B%0A%20%20%20%20%20%20%20%20context.executeQueryAsync(success%2C%20fail)%3B%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1747695%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20set%20property%20in%20property%20bag%20on%20root%20site%20collection%20using%20JSOM%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1747695%22%20slang%3D%22en-US%22%3ETry%20using%20PnPjs%2C%20see%20link%20below%20for%20more%20information%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fpnpjs%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fpnp.github.io%2Fpnpjs%2F%3C%2FA%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1749153%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20set%20property%20in%20property%20bag%20on%20root%20site%20collection%20using%20JSOM%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1749153%22%20slang%3D%22en-US%22%3E%3CP%3EI%20don't%20have%20any%20idea%20about%20PnPjs.%20Can%20you%20please%20tell%20me%20any%20other%20way%3FIts%20working%20fine%20if%20I%20install%20app%20on%20document%20center%20or%20developer%20site%20but%20it%20says%20access%20denied%20if%20I%20install%20it%20on%20root%20site%2C%20communication%20site%20or%20team%20site.%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F284426%22%20target%3D%22_blank%22%3E%40ArefHalmstrand%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

I am trying to update/create property in PropertyBag. Its working fine on classic experience (developer site and document center) but its not allowing application to start on modern site collections(root site, communication site and team site) and its not setting property in PropertyBag.

When I try to run application root site collection after installation its throwing this error

Access denied. You do not have permission to perform this action or access this resource.

Please tell me how can I set property in property bag for root site, communication site and team site.

 

var context = SP.ClientContext.get_current();
        var web = LawApp.Repositories.getWeb(context, hostUrl);
        var props = web.get_allProperties();

        props.set_item("CurrentVersion", 2002);
        web.update();
        context.executeQueryAsync(success, fail);

 

3 Replies
Highlighted
Try using PnPjs, see link below for more information:

https://pnp.github.io/pnpjs/
Highlighted

I don't have any idea about PnPjs. Can you please tell me any other way?Its working fine if I install app on document center or developer site but it says access denied if I install it on root site, communication site or team site.@ArefHalmstrand 

Highlighted

@Asjad_Ali 
I solved it by change the value of "CurrentVersion" to string because property bag does not take integer values. You can check this link too.

https://sharepoint.stackexchange.com/questions/171244/setting-propertybag-values-in-sharepoint-onlin...