1

I want to assign anonymous access to list items. For that I have assigned anonymous access to web application and "Lists and libraries" permissions to site collection.

Now to assign permission to list I have written code:

using (SPSite site = new SPSite(RootSiteUrl))
            {
                uint rootWebLanguage = site.RootWeb.Language;
                SPWebCollection allWebs = site.AllWebs;

                foreach (SPWeb currentWeb in allWebs)
                {
                    SPList list = currentWeb.Lists.TryGetList("listName");
                    if (list != null)
                    {
                        list.BreakRoleInheritance(true);
                        list.AllowEveryoneViewItems = true;
                        list.AnonymousPermMask = SPRights.ViewListItems;
                        //list.AnonymousPermMask64 = SPBasePermissions.ViewListItems;
                        list.Update();
                    }
                }
            }

This code will assign "View Items" permission to list but when I try to open list item in different browser then it is asking for login.

And if I will do same thing manually in that case I'm able to access list item anonymously.

It is not working using when I will try with coding.

2
  • Are you using custom master page or sharepoint default master page? because it might be happen it is asking login not for your list view but for some branding files which has not been published or not anonymously shared. Jun 5, 2015 at 8:57
  • yes, I have custom master page. But it works when I try same steps manually. Jun 5, 2015 at 8:59

1 Answer 1

2

Here is your answer.

using (SPSite site = new SPSite(RootSiteUrl))
{
uint rootWebLanguage = site.RootWeb.Language;
SPWebCollection allWebs = site.AllWebs;

foreach (SPWeb currentWeb in allWebs)
{
    SPList list = currentWeb.Lists.TryGetList("listName");
    if (list != null)
    {
          if (!list.HasUniqueRoleAssignments)
    {
        list.BreakRoleInheritance(true);
    }                    
    SPBasePermissions sPBasePermissions = SPBasePermissions.EmptyMask;
    SPBasePermissions basePermissions = web.RoleDefinitions.GetByType(SPRoleType.Guest).BasePermissions;
    {
        sPBasePermissions = (basePermissions & ~(SPBasePermissions.BrowseUserInfo | SPBasePermissions.UseRemoteAPIs));
        sPBasePermissions |= (SPBasePermissions.ViewListItems | SPBasePermissions.OpenItems | SPBasePermissions.ViewVersions | SPBasePermissions.Open);
    }
    list.AnonymousPermMask64 = sPBasePermissions;
    list.Update();
    }
}
}

Hope it will help you.

9
  • Thank you so much kalpesh vaghela......you code works for me. Jun 5, 2015 at 9:58
  • Hello Kalpesh, I need you help. I have given anonymous permission to pages library. I'm able to access my all page. But I'm not able to access one page which is created from page layout. So, can you please help me that how can I access my page which is created from page layout anonymously? Jun 5, 2015 at 11:49
  • are you using custom page layout or sharepoint OOTB page layout? Jun 5, 2015 at 11:51
  • I'm using custom pagelayout. Jun 5, 2015 at 12:00
  • Then first you should check like whether page layout is published, if you are loading scripts,images,styles in your page layout then those all should be published at least one time Jun 5, 2015 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.