I have a Discussion list in SharePoint online. It has few Questions and each question has few replies too.
I would like to know how to set "Best reply" using c# . List settings does not contain a column for best reply. So there must be some other way to set this value.
I could not find much information regarding this. Any pointer will be really valuable.
Just sample code for your reference.
var web = clientContext.Web;
var list = web.Lists.GetByTitle("DiscussionBoard");
//get disscussion by disscussion item id, for production, it should be based on caml query
var item = list.GetItemById(2);
//set best reply by reply item id, for production, it should be based on caml query
item["BestAnswerId"]=7;
item.Update();
clientContext.ExecuteQuery();
Related
What is the correct way to approve an item in WSS 3.0 through the object model? I have some code that was recently modified to auto approve items that met certain criteria by another developer. That developer has left however, and now we are finding that ALL items are being approved, regardless of whether or not they meet that criteria.
I'm pouring through the literally thousands of lines of code looking for where this is happening.
Alternately, is there a permission group that grants approval rights? I know the users the code runs as, so I could alter those if such a thing exists. However, despite finding some mention of it in the MS docs, on our own installation I don't see the permission listed anywhere.
Thanks. Please let me know if I can provide more information - I'm not well versed in SP programming.
YOu should search something like this.
SPList oList = oWeb.Lists["Documents"];
SPFolder oFolder = oList.RootFolder;
foreach(SPFile oFile in oFolder.Files)
{
if (oFile.Level == SPFileLevel.Draft)
{
oFile.Approve("");
}
}
How can I create a function that copies some item from one Sharepoint discussion board to another? Everything is quite simple except field name Posted By, MyEditor, and Editor.
If this is a one-time thing, you might look at using the Content Deployment API (there's a Content Deployment Wizard app that provides a UI for it).
Otherwise, you should be able to set the "posted by" by using something like this (assuming web is an open SPWeb and newItem is a discussion item):
SPUser user = web.Users["DOMAIN\\username"];
newItem[SPBuiltInFieldId.Author] = user.ID;
newItem[SPBuiltInFieldId.Editor] = user.ID;
newItem.SystemUpdate();
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.
I'm currently building an app that will parse all of the Audit entries in a site collection and send out a pretty email to users.
My problem is that the emails should be based on a particular web (essentially an email summarizing the changes that happened to each subsite). Apparently, there is no information in the SPAuditEntry object about the web it came from except for the DocLocation property.
This means I can get any of the following DocLocations (ItemType = Document, Event = Update):
sites/MySiteCollection/Documents/This is a test.doc
sites/MySiteCollection/Reporting Templates/audit.xml
sites/MySiteCollection/Lists/Reporting Metadata/1_.000
sites/MySiteCollection/MySubSite1/Lists/Announcements/2_.000
sites/MySiteCollection/MySubSite1/Template Documents/SampleTestEmail.doc
I'm thinking I can probably figure out the web from the URL by using SPSite.AllWebs.Names if I have to.
Q: How do I figure out which SPWeb a particular SPAuditEntry comes from?
I might have something (very crude), but it kinda depends on how deep your sub webs are nested. Are they just 1 level deep (i.e. site/web1, site/web2 or site/web1/web1_1 etc.). And have you looked if the SPAuditEntry objects have a ScopeId in their EventData xml? Found an article that describes kinda the same as you that uses the ScopeId from the EventData xml to do some matching:
Article
Also, the following post describes using the ItemId (guid) in an SPSiteDataQuery to retrieve the item, then uses the resulting data (WebId and ListId) to open a specific web / list. Might be a bit inefficient to retrieve an item at a time but it something...
Post
My solution is outlined below in pseudocode:
Get the collection of all web names in my site collection: _allWebNames = site.AllWebs.Names;
Since I only care about the top level SPWeb's inside the site, I parse the SPAuditEntry.DocLocation to get the possible web name. (Ex: will return "MySubSite1" out of "sites/MySiteCollection/MySubSite1/Lists/Announcements/2_.000")
string webName = GetPossibleWebName(SERVER_RELATIVE_URL, docLocation);
Then search my array to see if the web name matches.
index = Array.BinarySearch<string>(_allWebNames, webName, new ServiceSiteNameComparer());
If I find a match, then I can using SPSite.OpenWeb(string) to open the web and get the ID.
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();