Make the SharePoint document available to anonymous user
without enabling anonymous access at web application level or site collection
level. If we enable anonymous access at
top level then the content security will be lost. But without enabling anonymous at top level we
can make a document from document library or a list attachment to the public
users using SharePoint list property.
SharePoint list has a property “AllowEveryoneViewItems”,
which helps to make the document/list item attachment available to the public
users. This is the Boolean property. By
setting this property to “TRUE” we can allow public user to access the
document. But the public should access the direct document or list item
attachment url because the public users will be able to navigate though
SharePoint objects like document library or custom list.
Once the “AllowEveryoneViewItems” property set to “TRUE”
then we can user the document url in any place over web for public access.
The property can be set in different ways using back end scripts
or code but not through SharePoint UI.
1. Using Server Object Model
using (SPSite site = new SPSite("[site collection
url]"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = rootWeb.Lists.TryGetList("[list
title]");
if (list != null)
{
if (!list.AllowEveryoneViewItems)
{
list.AllowEveryoneViewItems = true;
list.Update();
}}}}
2. Using PowerShell Script
$web = Get-SPWeb -Identity "[site url]"
$list = $web.Lists.TryGetList("[list title]");
$list.AllowEveryoneViewItems = $true
$list.Update()
3. Using List Definition XML
Name="[list name]"
DisplayName="[list display name]"
Description="[list description]"
Type="10000"
BaseType="0"
Default="True"
VersioningEnabled="TRUE"
Hidden="FALSE"
AllowEveryoneViewItems="TRUE"
HiddenList="FALSE" />
It is important to note here that this property does not
apply to all list items, but only to documents in document libraries or to
attachments in list items. So you have to access this file directly for it to
work ! (reference: SPList.AllowEveryoneViewItems
property)
Here is the code snippet for accessing the file directly
using HttpWebRequest
var fileContent = String.Empty;
//construct path to the navigation file
string fileUrl = "[full url of the file]";
//create webrequest to fetch this file!
var httpRequest = (HttpWebRequest)WebRequest.Create(fileUrl);
using (var webResponse =
(HttpWebResponse)httpRequest.GetResponse())
{
if (webResponse.StatusCode == HttpStatusCode.OK)
{
using (var responseStream = new StreamReader(webResponse.GetResponseStream()))
{
//if we get the response, read the content from file as string
fileContent = responseStream.ReadToEnd();
}
}
}
No comments:
Post a Comment