i’m developing an interface to retrieve a file from sharepoint. i have the following method to upload a document:
public bool UploadDocument(string document)
{
if (String.IsNullOrEmpty(Path.GetFullPath(document)))
{
return false;
}
var site = GetNewDataContext(SiteUrl);
try
{
using (var clientContext = GetNewContext())
{
var uploadLocation = string.Format("{0}{1}/{2}", SiteUrl, Helpers.ListNames.RequestedDocuments, Path.GetFileName(document));
//Get Document List
var documentslist = clientContext.Web.Lists.GetByTitle(Helpers.ListNames.RequestedDocuments);
var fileCreationInformation = new FileCreationInformation
{
Content = System.IO.File.ReadAllBytes(document), //Assign to content byte[] i.e. documentStream
Overwrite = true, //Allow owerwrite of document
Url = uploadLocation //Upload URL
};
var uploadFile = documentslist.RootFolder.Files.Add(fileCreationInformation);
uploadFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();
}
site.SubmitChanges(ConflictMode.FailOnFirstConflict, true);
}
catch (Exception exception)
{
throw new ApplicationException(exception.Message);
}
return true;
}
However, my next step is to apply a custom permission.. i’ve found some blogs online which demonstrate how to do this:
http://howtosharepoint.blogspot.com/2010/10/programmatically-add-delete-modify-item.html
- Is it possible to set the permission in the uploading process?
- my understanding is, when the file is uploaded it inherits permissions from the list its uploaded to.
- my current download method returns a ListItem.. Is it possible to cast a ListItem to SPListItem? The reason for returning a SpListItem is due to the blog i posted above.. it appears SPListItem contains the permission info i need [to break and apply the new permission to].
my download method returns a ListItem.. how can i do this as an SPListItem
private static ListItemCollection GetListItemCollectionFromSp(string documentListName, string name, string value, string type, int rowLimit)
{
ListItemCollection listItems = null;
using (var ctx = GetNewContext())
{
var documentsList = ctx.Web.Lists.GetByTitle(documentListName);
var camlQuery = new CamlQuery
{
ViewXml =
@"<View>
<Query>
<Where>
<Eq>
<FieldRef Name='" + name + @"'/>
<Value Type='" + type + "'>" + value + @"</Value>
</Eq>
</Where>
<RowLimit>" + rowLimit + @"</RowLimit>
</Query>
</View>"
};
listItems = documentsList.GetItems(camlQuery);
ctx.Load(documentsList);
ctx.Load(listItems,
items => items.IncludeWithDefaultProperties(
item => item.DisplayName));
ctx.ExecuteQuery();
}
return listItems;
}
thanks in advance
Looks like you are trying to configure item level permissions in document library. Correct me if I am wrong.
Firstly, You have to stop inheriting permissions from parent site. You can then create a workflow and let it trigger when a document is uploaded. It is easy to design a workflow to configure permissions on item level than doing it in code. Maintenance would be easy too.