I'm writing a visual web part in visual studio for sharepoint 2010. The whole point of this web part is to change permissions around at the click of a button. I am able to access the permissions and output them to the screen. I'm able to change the permissions in the objects that I have, and then show the changed permissions on the screen. My problem is, after everything is done, no actual permissions have been changed.
webpart.TargetLibrary is a text field, inputted elsewhere as the name of the library I wish to investigate.
SPListCollection docLibs = m_SharePointWeb.GetListsOfType(SPBaseType.DocumentLibrary);
SPDocumentLibrary targetLib = (SPDocumentLibrary)(docLibs[webPart.TargetLibrary]);
SPListItemCollection libFolders = targetLib.Folders;
SPListItem folderItem = libFolders[0];
SPRoleAssignmentCollection folderRoles = folderItem.RoleAssignments;
SPRoleAssignment roleAssign = folderRoles[0];
SPRoleDefinitionBindingCollection spRDBC = roleAssign.RoleDefinitionBindings;
SPRoleDefinition Contribute = spRDBC[0].ParentWeb.RoleDefinitions["Contribute"];
folderItem.RoleAssignments[0].RoleDefinitionBindings.Add(Contribute);
folderItem.Update();
This is a somewhat simplified version - the original had some for loops and various bits of other code doing other things. Regardless, by everything I've been able to piece together from looking online, this code should add "Contribute" privs to the first role on the list in the first folder. As I mentioned, it does so to the local objects, but has no permanent effect. The Update() call appears to do nothing, and I'm not certain it's meant to be there in this case. I tried UpdateOverwriteVersion() - that doesn't do anything either. Any suggestions on what I might be doing wrong would be appreciated.
As a side note, it's not nearly so much of an issue, but I can't help the feeling that there's a more efficient and straightforward way to get a web object so that I can acquire role definitions by name. Any advice on that matter would also be appreciated.
Edit: solution moved to answer, below.
I have found answers to both the base and the side note.
For the side note, the following looks like it's the intended way to get an appropriate SPWeb object.
SPContext.Current.Web.RoleDefinitions["Contribute"];
For the base, the problem is that Sharepoint apparently does not save changing roles inside a SPRoleAssignment - only granting or removing permissions to the folder as a whole. This may or may not have to do with the fact that I was working inside a folder. In any case, in order to get it to save, you have to remove the person from SPRoleAssignmentCollection, make changes to their SPRoleAssignment, and then re-add the changed version. The correct version of the above code (integrating both answers) is as below.
SPRoleDefinition Contribute = SPContext.Current.Web.RoleDefinitions["Contribute"];
SPListCollection docLibs = m_SharePointWeb.GetListsOfType(SPBaseType.DocumentLibrary);
SPDocumentLibrary targetLib = (SPDocumentLibrary)(docLibs[webPart.TargetLibrary]);
SPListItemCollection libFolders = targetLib.Folders;
SPListItem folderItem = libFolders[0];
SPRoleAssignmentCollection folderRoles = folderItem.RoleAssignments;
SPRoleAssignment roleAssign = folderRoles[0];
folderRoles.Remove(roleAssign.Member);
roleAssign.RoleDefinitionBindings.Add(Contribute);
folderRoles.Add(roleAssign);
I'm just starting to use sharepoint designer and realised there's a lot that can be done to extend the basic features in sharepoint. We have an email alert sent out when a new task is created (by the user) and I want to customise the email so that it also includes a link called 'Assign'. When clicked, I want this link to automatically update the task with the assigned to field for the person that clicked it.
So I think the way to do this would be to hard-code the assign to value in the url behind this link, but I have no idea if this is possible or if there is an easier/better way to do this.
Any advice would be appreciated as I'm a complete beginner.
thanks.
I will not cover "How to modify the contents of an eamil alert" here as that is a seperate question and there are a lot of articles that cover that already.
For the Assigned link :-
You would need to create a custom page (or web part on an existing page) as the destination of your Assign link - this would take the Task ID as a query string param and then update the assigned to with the current user.
You could make this flexible by also taking the ListID but you may want to think about how this could be abused and put appropriate measures in place.
EDIT - in response to comment.
This is top of my head, not checked in compiler. This would have to sit on the same server as SharePoint to work as its using the OM - if you want to use a different server (why would you though) then look in the web services.
private void updateAssignedTo(Guid listId, int itemID)
{
SPWeb web = SPContent.Current.Web();
SPList list = web.Lists[listId];
SPListItem item = list.GetItemById(itemID);
item["Assigned To"] = web.CurrentUser;
item.Update();
}
You're going to have to work out how to get this code into to page or web part (SharePoint Designer is not going to cut it I think, you need Visual Studio) but its a starting point.
We use Sharepoint Services 3.0 as a project tracking tool. We have a list set up that contains your basic information (description, etc.) plus we have a "assigned person" column that is of type Person or Group that we use to associate list items with individuals. This column supports multiple selections.
We would like to set up alerts such that each person gets an alert email only if they are assigned to a list item. The approach we have taken is to set up a view on this list that is filtered to show list items where the assigned person equals [Me], and then to create an alert on this list that is set to send an email when someone changes an items that appears in the view.
This works well when there is only one person in the assigned person column. It does not work when there is more than one person in the assigned person column.
Does anybody know why this wouldn't work, or what I can do to troubleshoot? Is there a better way to achieve the end result? We could make several "assigned person" columns and not allow multiple selections, but that seems kind of kludgy.
Try this info site,
http://www.sharepointalert.info
it has a good alert trouble shooting guide.
The reason it works for one person but not multiple people is because the check is specifically against an individual. The comparison your view does is whether Assigned is equal to [Me], not if Assigned has [Me] as one of its entities.
Instead of using a list filter of is equal to, use the list filter contains. That should do the trick.
EDIT IN RESPONSE TO COMMENTS
To access the object model, you'll need to use Visual Studio. I'm unaware of a method to accomplish this kind of thing using SharePoint Designer, but maybe there's some sort of crazy Datasheet View thing you can do. Anyway... onto your actual needs...
The following code sample illustrates a very basic method for achieving your goal.
using (SPSite site = new SPSite("yourwebsiteurlhere"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["titleoflist"];
SPView view = list.Views["filteredviewname"];
view.Query = "<Where><Contains><FieldRef Name=\"assignfield\"/><Value Type=\"Integer\"><UserID Type=\"Integer\" /></Value></Contains></Where>";
view.Update();
}
}
Replace "yourwebsiteurlhere" with the website url, "titleoflist" with the title of your list in question, "filteredviewname" with the name of the view, and "assignfield" with the internal name that you used for your assignment field. If you created it through the standard SharePoint UI, this should be the name of the field without any spaces.
As far as where to run the code, you could put this kind of thing in a one-time workflow. I sometimes do that just to make sure I have necessary privileges. Hope this helps!
If you're not able to/allowed to use Visual Studio, then your solution will probably have to be to look into a 3rd party solution.
I'm having a hard time figuring out how to refer to a specific Item List within a list in SharePoint. I looked up the page in SharePoint Designer and found that the listitem is inside a custom made webpart inside a custom made webpage. I'm coding an event receiver and need to read the information that the user types into that listitem which is a textbox. Does anyone know the code to do this or how to get the guid for the specific list item?
I would appreciate any help I can get. I have tried looking all over the web for the answer. Thanks.
It might be a good idea to edit your question with exactly what you'd like to do with the information you read. However from what you've said so far:
The ID of the item being edited will already be passed through to the event receiver via SPItemEventProperties so there is no need to look it up. If you need to look up a different item in the list (or indeed in a different list altogether), the Accessing list items using the object model page on SharePoint Dev Wiki gives you all of the options. A good general rule is use SPQuery to get best performance on the whole.
Note: There is a pretty good page on the SharePoint Dev Wiki demonstrating how to write an event receiver. It shows how to query and obtain a list item title.
Update after comments:
Once you have an SPListItem object, you can find its GUID through the UniqueId property. In the "Accessing lists" wiki link I've provided above the code samples show how to use the Title property.
Every piece of data you need to access within SharePoint should be available through the object model. This is a simplification, but generally the pages themselves are rendered from template files on the server and combined with data in the database to display to the user. So editing the page programmatically or through its source isn't going to work.
Apologies if I'm making an incorrect assumption but you sound fairly new to SharePoint development. I strongly recommend you read at least the first few chapters of Inside Windows SharePoint Services 3.0 as the inner workings of SharePoint are important to get a good understanding of and this book should help a lot. There is a section of event receivers in it as well.
Have you looked at SharePoint.ListsService Webservice?
string url = "WSS Site URL";
SharePoint.ListsService.Lists lists = SharePoint.ListsService.Lists(url);
XmlNode list = lists.GetList("ListName");
XmlNode xlists = lists.GetListCollection();
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I want to allow a certain group of users to add items to a list, but not be able to view all items. This is so I can set up a workflow with certain parts of it private. I thought it'd be possible by defining a new permission level in:
http://servername/_layouts/addrole.aspx ('Add a permission level' page)
However, when you select the "add items" list permission, "view items" is automatically ticked also.
Anyone know a solution to this?
As a half-way option you can set up the list to only show items to their owner (Settings > Advanced Settings and then set options for Read Access / Edit Access as "Only their own". This won't preclude a person from seeing all items that were added by them, but there won't be anything viewable outside of this permission (other than by a list owner).
The View Items is a dependent permission for Add Items so not sure if we can add such permissions OOB in sharepoint, have a look here :
(http://office.microsoft.com/en-us/sharepointtechnology/HA101001491033.aspx)
You can have a dirty workaround of creating 2 lists and than adding the code in the item added event of the first list to add item to another list and than remove it from the first list, not sure if this is a good solution . . .
$spweb=Get-SPWeb -Identity "<site url>";
$spRoleDefinition = New-Object Microsoft.SharePoint.SPRoleDefinition;
$spRoleDefinition.Name = "Submit only";
$spRoleDefinition.Description = "Can submit/add forms/files/items into library or list but cannot view/edit them.";
$spRoleDefinition.BasePermissions = "AddListItems, ViewPages, ViewFormPages, Open";
$spweb.RoleDefinitions.Add($spRoleDefinition);
Taken from:
http://sajiviswam.wordpress.com/2011/12/09/add-only-permission-level-in-sharepoint-2010/
I had a similar problem, where didn't want anonymous users seeing contents of list.
The same solution might work for this.
In SharePoint designer (for some reason couldn't edit page on web), open the DispForm.aspx page and on webpart properties, add a target audience (if want no one to see make webpart hidden) DO NOT DELETE WEBPART - doing this breaks your list totally!
Can do the same with AllItems.aspx
Hope this helps.
Use an impersonation step in the workflow.
Out of the box with SharePoint designer I can only think to use a workflow to move any items from a Public "dropbox" list to a secured list.
A user can see and upload items to the public dropbox, but immediately a workflow kicks off that just moves the content to another, identical, secured list. You can decide if you need to allow content overwriting or not.
A hacky workaround, but that without programming that is all SharePoint is. (My company won't let me write code to it yet)
You didn't really specify which kind of list you're using, but if you look in the list settings under "Advanced Settings" you'll probably find an "Item Level Permissions" section. This will let you choose to limit users to reading (or editing) only the items they've submitted. This goes above and beyond any other ACLs set on the list or it's items.
I think use Advanced permission is not accessible since it can not prevent the one who submits from view it, otherwise it is a good solution!
Workflow should, I think, can do the job. Just make sure when an item uploaded the worklow is triggered. Then if you can build a workflow which can set specific permission to the item, all thing should be done.
If you do not get your hand dirty with building workflow then go to 3w.sharepointboost.com when have a sort of plug and play solution called Column View Permission.
I was just working on a quick solution for this, doing research when I found this message.
Besides the SPD workflow, will not work with anonymous users, I was thinking of doing a infopath html form that mails the form to a forms library. You can have one form library as the site to start the form and then have the results stored in a different forms library. Since you can set the form library to accept email from anyone you can prevent people from reading but they can still edit.
Have not tried this but if I run into problems will post comments.
I completely agree with 'Ceesaaxp'. Under Advanced Settings for the list, set Read Access to Only Their Own. I created a Knowledge Management process, whereby I created two lists, one for pending knowledge articles, and one for approved. I modified the 'New Form' page for the Pending list and disabled a drop down box using JavaScript, which was used as the status of the article. This drop down is then set permanently as 'Pending'. I also created a new permission level which allows users to Add items only. I then created a workflow which moves the article into the Approved list when the status drop down box is set to 'Approved'.
I then changed the read only settings in advanced settings of the pending list to only their own, so all knowledge articles are approved before they are published.
#Jomit. Your workaround may work, but it has the racing condition problem. Users may still have a chance to see other items. This may be a no-no depending on your rules.
Regular lists in SharePoint offer this option under Settings/Advanced Settings/Item-Level Permissions. Albeit for some reason this option is missing from the GUI for Document and Form Libraries.
One possible solution is to write a simple program to make these changes using the SharePoint Object Model.
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splist.writesecurity.aspx
// Sample code for setting writing and reading security on a form library
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite(args[0]))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists[args[1]];
list.WriteSecurity = 2;
list.ReadSecurity = 2;
list.Update();
}
}
}
}