Automate Permissions for a document library

I have developed some code that only works if one of the two site administrators run it. I get error "The web application athttp://mySite not found". The error comes when I try to run

SPSite WebApp =newSPSite(http://dev);

I nested the line above inside ofSPSecurity.RunWithElevatedPrivileges(delegate()...which caused a new error, nullReferenceException which highlighted SPSecurity.RunWithElevatedPrivileges(delegate().

My code is fired off by theItemAdded event. Sample of my code below (please note that this does function when logged in as an administrator, but not as a regular user).

SPSecurity.RunWithElevatedPrivileges(delegate()

{

SPSite WebApp =newSPSite("http://dev");

SPWeb web = WebApp.OpenWeb();

SPUser user = web.Users.GetByID(1073741823);

string userEmail = @user.Email;

string userName = @user.Name;

string loginName = @user.LoginName;

// SPUser user = web.Users.GetByID(1073741823);

using (SPSite site =newSPSite(@"http://dev"))

//using (SPSite site = new SPSite(this.Page.Request.Url.ToString()))

{

using (SPWeb thisWeb = site.OpenWeb())

{

//base.ItemAdded(properties);

//this.DisableEventFiring();

thisWeb.AllowUnsafeUpdates =true;

SPList theList = thisWeb.Lists["Leadsheets"];

//SPFile documentFile = properties.ListItem.File;

SPListItem record = theList.Items[1];

record.BreakRoleInheritance(false);

//ORIGINAL RECORD SPListItem record = theList.Items[1];

//SPListItem record = theList.Items.Add();

record[0] = user;

record.Update();// uses SHAREPOINT\system

SPRoleAssignmentCollection perms = record.RoleAssignments;

SPRoleDefinitionCollection roleDefinitions = thisWeb.RoleDefinitions;

int count = perms.Count;

//Remove all permissions

for (int j = 0; j < count; j++)

{

perms.Remove(0);

}

//Add permissions

site.AllowUnsafeUpdates =true;

site.CatchAccessDeniedException =true;

SPRoleAssignment roleAssignment =newSPRoleAssignment(@"xx\ron","ron@ddd.com","Ron Touple","no notes");

SPRoleAssignment roleAssignment1 =newSPRoleAssignment(@"xx\bmackey","bmackey@ddd.com","Brian Mackey","no notes");

SPRoleAssignment roleAssignment2 =newSPRoleAssignment(@"xx\brad","brad@ddd.com","Brad Cook","no notes");

SPRoleAssignment roleAssignment3 =newSPRoleAssignment(loginName, userEmail, userName,"no notes");

SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;

SPRoleDefinitionBindingCollection roleDefBindings1 = roleAssignment1.RoleDefinitionBindings;

SPRoleDefinitionBindingCollection roleDefBindings2 = roleAssignment2.RoleDefinitionBindings;

SPRoleDefinitionBindingCollection roleDefBindings3 = roleAssignment3.RoleDefinitionBindings;

roleDefBindings.Add(roleDefinitions["Contribute"]);

roleDefBindings1.Add(roleDefinitions["Contribute"]);

roleDefBindings2.Add(roleDefinitions["Contribute"]);

roleDefBindings3.Add(roleDefinitions["Contribute"]);

perms.Add(roleAssignment);

perms.Add(roleAssignment1);

perms.Add(roleAssignment2);

perms.Add(roleAssignment3);

}

}

// this.EnableEventFiring();

});

[7255 byte] By [smacky311] at [2008-1-10]
# 1
When you get the nullreference exception are you running as a "regular user". What is a "regular user". In order for the runwitheleveatedprivileges to run it must be called by a SharePoint user.
smc750 at 2007-10-3 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 2

Update: I created a log and determined that my event is not firing unless I'm logged in as one of the two site administrators. Any ideas?

smacky311 at 2007-10-3 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 3
bump
smacky311 at 2007-10-3 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 4
What is a regular user? Where is your event not firing from. Is this a custom event handler added to a document library. Maybe the user does not have rights to update the item so the event does not fire?
smc750 at 2007-10-3 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 5

Regular user: Non-site collection administrator. I tried a user w/ site administrator priviladges and full control of the forms library I was trying to get him to post to.

The event is an ItemAdded event that is supposed to fire from a forms library named Leadsheets. This is a custom event handler.

Items are added to the list, the permissions do not update.

smacky311 at 2007-10-3 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 6
Are you still elevating permissions in your code?. Regular users wont have rights to add permissions, therefore when elevating permissions the app pool account must have permissions. Can you make your app pool account a site administrator and run with elevated permissions and see if it works.
smc750 at 2007-10-3 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 7
That is a clever suggestion. I changed the identity of the app-pool to myself and reset IIS. Unfortunetly, the code still does not work. Some of the code is based on existing code from a Microsoft book, so there might be something happening before the event is fired that is causing the code to break.
smacky311 at 2007-10-3 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 8
app pool accounts must have dbo owner rights to the content database it is associated with in sql server. It must also have read/write permissions to the sharepoint config and admin databases. Make sure these are set correctly and try again.
smc750 at 2007-10-3 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 9
I'm going to have to wait on that, my boss doesnt want me to have access to the SQL db yet. You think that could still be the problem even though the code fires when I'm logged in as myself?
smacky311 at 2007-10-3 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 10
It is probably failing and the error is not being caught when using a regular user. Do you have access to the site collection? Can you add the app pool account as a site collection administrator?
smc750 at 2007-10-3 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...

SharePoint Products and Technologies

Site Classified