0

I am new to SharePoint development, I am building an internet facing website with anonymous permissions for the pages, my current situation is that my lists are accessible without any security upon them and when I tried adjusting the permission on the lists and libraries to have certain permissions my code couldn't read or write from the lists.

Can anyone help me achieve a point where the pages are accessible without SharePoint security while the lists and libraries aren't?

Here is an example of how I read from the lists

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite newSite = new SPSite(SPControl.GetContextSite(Context).Url))
    {
        using (SPWeb web = newSite.RootWeb)
        {
            configurationslist = web.Lists[configurationListName];
            q.Query = "<Where>";
            q.Query += "<Eq>";
            q.Query += "<FieldRef Name='LinkTitle' />";
            q.Query += "<Value Type='Text'>" + key + "</Value>";
            q.Query += "</Eq>";
            q.Query += "</Where>";
            try
            {
                SPListItemCollection listCollection = configurationslist.GetItems(q);
                if (listCollection.Count > 0)
                {
                    foreach (SPListItem item in listCollection)
                    {
                        returnedValue = getStringValue(item["Value"]);
                        result = true;
                    }
                }
                else
                {
                    returnedValue = "";
                    result = false;
                }
            }
            catch (Exception e)
            {
                returnedValue = "";
                result = false;
            }
        }
    }
});
1
  • What site template are you using? You should use Publishing Site Template and create Publishing Pages in OOB Pages Library. Commented Apr 30, 2015 at 13:14

1 Answer 1

1

I am assuming that you are using your given code and SharePoint 2013 for your question.

You are using SPSecurity.RunWithElevatedPrivileges(delegate().. that means you can have full access to the operation which you are performing inside that code blocks, this will use Application pool credentials automatically to perform the operations. Take a look at MSDN article here for more detail about it : SPSecurity.RunWithElevatedPrivileges Method

So with that code blocks, you will not be asked for authentication.

To do access SharePoint pages without authentication, you need to enable the anonymous access for that site collection.

This article, How to enable anonymous access in SharePoint 2013 explains almost every aspects of Anonymous access at different levels.

When you apply anonymous access for site collection, the site pages or publishing pages library will be accessible without authentication, and the list/library will be only read only that you can also change in the list/library settings.

Hope this Helps!

4
  • I am using sharepoint 2010, the issue is I don't want the lists and libraries to even have a read only access, I want anyone trying to access the site assets to be prompted to log in. so when the list don't have anonymous access I can't access it from my code Commented Apr 30, 2015 at 13:34
  • You can also make that library not readable as well. For that you need to change in list setting and then permissions. And yes, if list doesn't have the permission for anonymous access then you should use that RunWithElevatedPrivileges method, it should work. Commented Apr 30, 2015 at 13:38
  • let me explain a little more, the situation is as follows lists, libraries, and site pages are anonymous, and I read from them using the mentioned code. when I prevent anonymous access to lists and libraries. the code prompts me to log in to sharepoint. I want to be able to log in programmatically. Can I do that ? Commented Apr 30, 2015 at 13:48
  • Yes, you can do that. But I wonder why you are being asked for credentials when you are using the RunWithElevatedPrivileges method. Commented May 1, 2015 at 7:11

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.