Monday, March 22, 2010
Code Snippets
Get list Items:
/// Gets a collection of list items that match the query from the specified list, regardless of the permissions of the current user
/// The server-relative path to the list
/// The CAML query that has to be run against the list
/// An SPListItemCollection object that represents the resulting list items
public static SPListItemCollection GetSecuredListItems(string ListServerRelativePath, string CamlQuery)
{
if (string.IsNullOrEmpty(ListServerRelativePath))
throw new ArgumentNullException("The server-relative path to the list is required for this function");
SPListItemCollection results = null;
string listAbsoluteURL = SPContext.Current.Site.MakeFullUrl(ListServerRelativePath);
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(listAbsoluteURL))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.GetList(ListServerRelativePath);
SPQuery query = new SPQuery();
query.Query = CamlQuery;
results = list.GetItems(query);
}
}
});
return results;
}
Update List Item:
/// Updates a single item for a single field on a list, even if the current user doesn't have the access
/// The server-relative path to the list
/// The internal ID of the item to be updated
/// The display name of the field to be updated
/// The new value that has to be applied for the field
/// The SPListItem object that contains the list item that was updated
public static SPListItem UpdateSecuredListItem(string ListServerRelativePath, string ID, string FieldName, string Value)
{
if (string.IsNullOrEmpty(ListServerRelativePath))
throw new ArgumentNullException("The server-relative path to the list is required for this function");
int id;
if (!int.TryParse(ID, out id))
throw new ArgumentException("The ID specified is not valid (ID = " + ID + ").");
SPListItem result = null;
string listAbsoluteURL = SPContext.Current.Site.MakeFullUrl(ListServerRelativePath);
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(listAbsoluteURL))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList list = web.GetList(ListServerRelativePath);
SPField field = list.Fields[FieldName];
if (field == null)
throw new ArgumentException("The FieldName specified (" + FieldName + ") is not valid for the List (" + list.Title + ").");
result = list.GetItemById(id);
if (result != null)
result[FieldName] = Value;
else
throw new ArgumentException("There is no listitem with the specified ID (ID = " + ID + ").");
result.Update();
web.AllowUnsafeUpdates = false;
}
}
});
return result;
}
Delete List Permissions
namespace DeleteListPermissions
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("\n\n Invocation: DeleteListPermissions ");
return;
}
//Get SPWeb object
SPSite Site = new SPSite(args[0]); //e.g., "http://myserver/mysite"
SPWeb Web = Site.OpenWeb();
//Get SPListItem
SPList List = Web.Lists[args[1]]; //e.g., "Announcements"
Console.WriteLine("\n\nList " + args[1] + " found-- Starting Permission Deletion");
//Check for permission inheritance, and break if necessary
if (!List.HasUniqueRoleAssignments)
{
List.BreakRoleInheritance(false); //pass true to copy role assignments from parent, false to start from scratch
}
List.Update();
Console.WriteLine("Done....");
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment