Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Halle 9 posts 59 karma points
    Mar 17, 2015 @ 13:19

    Programmatically add Public Access to document


    Which API do I need to use if i want to add Public Access, i.e. a member group to a specific document/node programmatically?

    Currently I do like this:

    Access.AddMembershipRoleToDocument(document.Id, newMemberRole);

    ...and it works, except that it throws an exception in the backoffice which is not good, I guess?

    Document doesn't contain a memberId. This might be caused if document is protected using umbraco RC1 or older.

    I tried to use this method but it is deprecated:

    Access.AddMemberToDocument(document.Id, member.Id);

    Please help me out.

    EDIT: I'm using version 7.2.2.

    Best Regards,
    Hampus Holmström


  • Halle 9 posts 59 karma points
    Mar 18, 2015 @ 12:43

    I found a solution, It was actually caused by the setting the property simple to true in the ProtectedPage method i invoked before doing the stuff in my previous post.

    This was the code i tried, which didn't work properly:

    Access.ProtectPage(true, document.Id, 0, 0);

    This was the code that made it work:

    Access.ProtectPage(false, document.Id, 0, 0);

    No more errors on the public access page after this fix.

    Best Regards,

  • Lee 1117 posts 3051 karma points
    Jan 15, 2016 @ 14:47

    In case anyone else does this, and finds Access.ProtectPage() is obsolete, the equivalent way of doing it using the PublicAccessService is as follows.

            var registrationPage = ApplicationContext.Current.Services.ContentService.GetById(MYID);
            var pageToRestrictAccessTo= ApplicationContext.Current.Services.ContentService.GetById(MYID);
            var entry = new PublicAccessEntry(pageToRestrictAccessTo, registrationPage, registrationPage, new List<PublicAccessRule>());

    And then to add a specific role

    ApplicationContext.Current.Services.PublicAccessService.AddRule(pageToRestrictAccessTo, Umbraco.Core.Constants.Conventions.PublicAccess.MemberRoleRuleType, "MyRole"); 
  • mouseball 62 posts 65 karma points
    Nov 09, 2019 @ 03:51

    I've attempted to adapt this to Umbraco 8... but it shanks it so badly you have to restore the database to recover from the dreaded "Value cannot be null. Parameter name: source"

    What is wrong with this code?

    var registrationPage = Services.ContentService.GetById(1234);
    var pageToRestrictAccessTo = Services.ContentService.GetById(5678);
    var entry = new PublicAccessEntry(pageToRestrictAccessTo, registrationPage, registrationPage, new List<PublicAccessRule>());

    Note that I am using a surface controller - I haven't made it as far as adding a rule yet because it wigs out before that, imagine it will be something like

    Services.PublicAccessService.AddRule(pageToRestrictAccessTo, Constants.Conventions.PublicAccess.MemberRoleRuleType, "MyGroup");
Please Sign in or register to post replies

Write your reply to: