Weird SPListItem URL when adding a new announcement - sharepoint

When I add a new item in a default Announcement list using the GUI the new item looks just as I would expect, showing the title of the item in the breadcrumb. However when adding the new item through the object model:
SPList theList = web.Lists["announcement"];
SPListItem theitem = theList.Add();
theitem["Title"] = "this is the title";
theitem.Update();
then the breadcrumb will display something like
WebTitle.ListTitle.34_.000, which I believe to be the itemID and versionnumber of the item.
A search give this but I would like to know the root cause and possible how to avoid the issue.

The issue has been resolved. Actually the list in question was a discussion list, not a announcement list ( why it was then named "announcement" is a big mystery),
Since a discussion is actually a thread container and the threads within the container, I had to call SPUtility.CreateNewDiscussion rather then list.Add.

Related

SharePoint WSS3 - create document with a specific content type

I'm trying to create a new document in a SharePoint (WSS 3.0) document library with a specific content type. But the new document always has the default content type for the doc lib, not the custom one I specified :-(
I'm using SPFileCollection.Add to create the file, and using the properties Hashtable to pass the content type ID/name:
SPList list = web.Lists["Test Doc Lib"];
Hashtable properties = new Hashtable();
properties.Add("ContentType", "MyCustomContentType");
properties.Add("ContentTypeId", list.ContentTypes["MyCustomContentType"].Id.ToString());
list.RootFolder.Files.Add("Test File.html", Encoding.UTF8.GetBytes("test"), properties);
All the sample code I can find uses SPFileCollection.Add to initially create the file, then re-fetches the new SPListItem and sets the content type on this. I don't want to do this because it would cause both ItemAdding and ItemUpdating events to fire.
This works for me.
SPContentTypeId id=list.ParentWeb.AvailableContentTypes["ContentTypeName"].Id;
Hashtable htMetaData = new Hashtable();
htMetaData.Add("ContentTypeId", id.ToString());
SPFile newfile = docSetFolder.Files.Add(fullFileUrl,fileByteArray,htMetaData,true);
I have never tried to add file in your way, but IMHO you can create file and then change list item content type. You can disable event firing to prevent ItemAdding & ItemUpdating events from firing. There're many articles about this (search for "sharepoint disable event firing"). Some links:
http://blogs.syrinx.com/blogs/sharepoint/archive/2008/11/10/disabling-event-firing-on-splistitem-update-differently.aspx
http://anylookup.com/how-to-really-disable-event-firing-in-sharepoint

SPContext.Current.Web is not latest

I am creating a list with a deployed list template. with the following code:
SPSite site = new SPSite("http://servername");
SPWeb web = site.OpenWeb();
web.Lists.Add(listName, listName, listTemplate);
web.Update();
SPList List = Web.Lists[listName];
I am able to access the list with the web object which is used to create it. But, SPContext.Current.Web is not updated. So, the following throws error:
SPContext.Current.Web.Lists[listName]
Is it possible to update the SPContext.Current object with latest information so that the list accessible after it is created?
Thanks in advance!
Update: Code updated.
Your code doesn't show this, so I'm going to ask for the obvious: did you try calling web.Update() right after adding the new list?
I added the below line after web.Update() and it started working.
SPContext myContext = SPContext.GetContext(Web);

Using an event receiver to update an item's 'Title' field

I have written an event receiver that is activated on the 'ItemAdded' event of a 'Pages' list. The code runs fine, but my issue is this - during the call to my Sub ItemAdded, I want to change the value of a field belonging to the current list item (in this case an aspx page).
The idea is that I can configure the 'Title' field to be another value, which my event receiver configures, and by the time the user sees the page in edit mode, the new title will have been saved for the page. Here is my current attempt:
Public Overrides Sub ItemAdded(ByVal properties As Microsoft.SharePoint.SPItemEventProperties)
Dim newItem As SPListItem = properties.ListItem
Dim currentSiteTitle As String = properties.OpenWeb().Title
UpdateItemTitle(newItem, currentSiteTitle)
newItem.Update()
'newItem.SystemUpdate()
End Sub
As you can see, I've tried both Update() and SystemUpdate(), but in each case, when the user tried to check in the page, they get a message that the page has been modified externally. Plus, when viewing the page, the title field value has not changed.
Is what I'm trying to do at all possible, or is there a better way?
Cheers.
Jas.
changing the properties.afterproperties will get around the save conflict. note that the afterproperties is a hashtable, but you can access it so:
properties.AfterProperties["Title"] = "My New Title";
ItemAdded Name says it all. It a Asynchronous Event that happens after the Items has been added, so is the issue with your calse. I suggest you to hook the ItemAdding event unless you have reason not to do so.
Refer the Link for Details on Asynchronous & Synchronous
The best way to accomplish this is use the ItemAdding event. This will allow you to change the Title value before it is saved to the database. Trying to change them in ItemAdded is possible but will cause the headaches you are experiencing.
Have you tried ItemAdding?
Now following on from the above, I found that when I was working with a discussion group the only place I could change any fields in the list item was in the ItemUpdating method, where I could assign the new value into the properties.AfterProperties hash corresponding to the item name as mentioned previously.
Unfortunately this didn't seem to automatically be run when a new reply was added to the discussion ( maybe it is in other list related scenarios ) but if I put code into the ItemAdded method ( ItemAdding was not being triggered either ) I found that it was being run but that I couldn't change the item from there, so I ended up with something like this in itemAdded:
public override void ItemAdded(SPItemEventProperties properties)
{
SPListItem item = properties.ListItem;
item.Update();
}
The outcome of this is that the field is updated but not before it is shown so that when the user is directed to the output page the list will look as it did before but if you change view or look at the details for the reply you will find that it has actually changed as it was supposed to.
This is good enough for me as I'm expecting most of my replies to arrive by email.

Person & Group custom field type sharepoint

I have created a custom field type as it is there in sharepoint OOTB, the difference is only that the end user does not need to check the name i.e I have replaced it with DropDownList. The dropdownlist suggest the no. of users available in the web site for that I have created a FieldClass which inherits from SPFieldUser and a FieldControlClass which inherits from UserField. It is working fine in all conditions i.e when I create a List or Document Libarary it shows me the DropDownList
with respective users after saying OK it creates an item for me. I have overriden a Value property in FieldControlClass as follows,
public override object Value
{
get
{
SPUserCollection userscollection = rootWeb.SiteUsers;
//ddlInfoBox is a DropDownList to which I have Binded the collection of users in the form of string
SPUser user = userscollection.Web.EnsureUser(this.ddlInfoBox.SelectedValue);
SPFieldUserValue userval = new SPFieldUserValue(user.ParentWeb, user.ID, user.LoginName);
return userval;
}
set
{
SPFieldUserValue userval = (SPFieldUserValue) this.ItemFieldValue;
this.ddlInfoBox.SelectedValue = userval.Lookupvalue; //Here look up value is nothing but a Login name e.g In-Wai-Svr2\tjagtap
}
}
Due to above property the Custom Field's Value for this current ListItem will be stored as SPFieldUserValue e.g 27#;In-Wai-Svr2\tjagtap.
The main problem is here, when this particular ListItem is shown in the list page views e.g on AllItems.aspx or the custom view pages associated with it, it shows the
number as 27 as a FieldValue insted of HyperLink with text as "In-Wai-Svr2\tjagtap" and PostBackURL as "/_layouts/userdisp.aspx?ID=27".
When I edit this Item it makes the respective value selected in the dropdownlist, also while viewing this item i.e on DispForm.aspx it also shows the hyperlink. I have
acheived it by writting a custom logic in createchildcontrol() method i.e by using ControlMode if it is New or Edit then fill the dropdown list, if it is Display then get the ItemFieldValue Type Cast it into SPFieldUserValue and get corresponding lookupid and value for making the URL and showing Text of the HyperLink.
I have spent a lot of time on searching and bringing the HyperLink as the user name with navigation insted of UserID (27) as a string on the list view pages e.g AllItem.aspx but to no avail, then after a lot of research I found that there might be a way of achieving such kind of functionality by using field type definition xml file where there is a provision to define a DisplayPatteren as you wish by specifying the html code. But here is a problem How can I get the UserID (27) with respective UserName e.g In-Wai-Svr2\tjagtap inorder to make an anchor tag like In-Wai-Svr2\tjagtap which will solve my problem. I have hard coded this anchor tag within the Default case statement of a switch under DisplayPatteren but it shows me the field value on AllItems.aspx as
In-Wai-Svr2\tjagtap27 i.e the value defined in xml file is concatenating with the string value (27).
Please help me to resolve the above mentioned 2 issue. I am really in need of solving this problem ASAP.
Thanks & Regards,
Tejas Jagtap
Have u tried to override the GetFieldValueAsHtml() method in the the custom field class or maybe the RenderFieldForDisplay() method in the custom field control class.
Can you use the DisplayPattern CAML from the User field type?

Sharepoint default list item detail view

I have a webpart that renders random list items (from any list and list type) in a specified format. I want the items that are being displayed in the webpart to link to their ListItem detail views. However, I don't see a property on the list itself that would tell me what view is the default DETAIL view for the list (ie. blog list detail is Post.aspx). Does this come from the list definition? How would I get that information programmatically? I'm trying to avoid hard-coding any list information based on list type.
Have a look at SPList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url.
I think this is what you're looking for. You'll need to append the the SPListItem.ID on the querystring so that it knows which list item to display.
using (SPWeb myWeb = GetMyWeb()) // GetMyWeb gets a reference to a SPWeb object
{
SPList myList = GetMyList(myWeb); // GetMyList gets a reference to a SPList object
SPListItem myItem = GetMyListItem(myList); // GetMyListItem gets a reference to a SPListItem object
string url = String.Format("{0}/{1}?ID={2}",
myWeb.Url,
myList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url,
myItem.ID);
}
It's also a good practice to append &Source=/url/to/current/page to the querystring so that users will be redirected back to the page they left when they click the Cancel/Close buttons on the Edit or Display forms.

Resources