10/03/2013

Granting Anonymous Access to Sharepoint List / Library Programtically

CodeProject
In this post we will see how to enable and disable Anonymous access to a List or Document library from a sharepoint feature (C# code).

There are 3 different steps involved in granting Anonymous access to a List/Library in a right way.

Step 1: Enabling the anonymous accesss to the Web application from Central Admin. This does not mean that the whole web application is now anonymous. This step signifies that, this web application has ability to host resources that can be accessed by Anonymous user.

Now click save and close Central Admin. I am pointing again that the above step will not make whole webapplication accessible to anonymous users. This will create scope for hosting something that can be accessible by anonymous user.

Step 2: Enabling the Anonymous permissions in Web application settings. Go to Site Permission screen. As you have already enabled Anonymous access for this web application from central admin, a new button "Anonymnous Access" will be appeared on the ribbon.

This will open a popup with 3 options shown below specifying whether you want to enable access on whole site, or only lists and libraries or nothing.

I selected Lists and Libraries. Even now it doesnt mean that Lists and libraries in my web application are accessible to Anonymous user. here comes the next step.

Step 3: Now open the List / Library for which you want Anonymous access, and go to List / Library Permissions.

Now, click on Anonymous Access button on ribbon, and a popup will appear where you can select which kind of permissions you want to give for Unauthenticated user.

Now, the list can be accessed by unauthenticated / anonymous users.

But, in real time scenarios, the step 3 is not recomended as it involves manual intervention in granting access, which is not possible in most of the PRODUCTION scenarios.

So, it is recomended to perform step 3 using a feature activation or de-activation.
Here is the code for making a Document library anonymous while activating a feature.

Here is the code for removing Anonymous access to a Document library while de-activating a feature.