Tag Archives: SPListItem

The difference between web parts and lists

Did you try adding another web part to a page and it displays the same data as another web part on the page? Here is the answer.

As part of my habit of posting in this site an answer for any question I see more than once in the forums, I want to share with you an explanation I use in the forums to let sharepoint beginners realize what is the difference between a web part and a list.

The confusion sounds like this:
I’m a newbie to the SharePoint world so if this is a really amateur question, you know why. I am currently tasked with setting up the SharePoint site for my department. I have tried to put two instances of the Links web part on the top-level of my site. It will allow me to do this but when I make a change to one of the web parts the change also occurs in the other Links web part.” (taken from an msdn forum post)

To which I reply:
You are confusing “web parts” and “lists
A “web part” is a mechanism to display data, while a “list” is like a small database – a mechanism to store data.

What you did, is have one list (links, or contacts) and two web part displaying the same list – same data.

If you want two different sets of data then the easy way would be to create another list, and add a web part to look into that list.

Another option is to create a field in the list by the name (for example) “show in web part 1” of type boolean, and then configure the two webparts that show the same information to show based on that field (this method is known as a filter).

Advertisements

How to copy attachments from one list item to another

The microsoft support article for WSS 2 shows us how to download attachments from a list item. Basically, the attachments for a list item are stored as SPFile objects under a hidden folder in the list where those attachments are (a folder called “Attachments”) – where each list item that has an attachment has its own folder – with the ID of the item being the folder’s name. Here is a code sample for a function to copy attachments from one item to another.

private void CopyAttachments(SPListItem sourceItem, SPListItem targetItem){

try {//get the folder with the attachments for the source item

SPFolder sourceItemAttachmentsFolder = sourceItem.Web.Folders[“Lists”].SubFolders[sourceItem.ParentList.Title] .SubFolders[“Attachments”].SubFolders[sourceItem.ID.ToString()];

//Loop over the attachments, and add them to the target item

foreach (SPFile file in sourceItemAttachmentsFolder.Files)

{byte[] binFile = file.OpenBinary();

targetItem.Attachments.AddNow(file.Name, binFile);}

} catch { }finally { sourceItem.Web.Dispose();}}

Ref : Sharepoint Tips And Tricks

Event handler to archive items when deleted

The requirement – copy a list item that is deleted to an archive list. The solution – event receiver that copies the item. The code below does the job, but again is only a sample – you still need to implement error handling, and there are hard coded variables there you will want to change.

Note: this code is specifically for list items, not for documents in document libraries. You can easily change the code to support documents as well (see the CopyAttachments function for an example on how to copy files).

public override void ItemDeleting(SPItemEventProperties properties) {

//note: may require permission elevation. //TODO: add error handling //get the item being deleted

SPListItem item = properties.ListItem;

//get the target list

SPList targetList = properties.Web.Lists[“Announcements Archive”];

//create the new item

SPListItem newItem = targetList.Items.Add();

//copy the list item to the target

foreach (SPField f in item.Fields) {

if (!f.ReadOnlyField && newItem.Fields.ContainsField(f.InternalName)) newItem[newItem.Fields.GetFieldByInternalName(f.InternalName).Id] = item[f.Id]; }

//copy “special” read only fields that can be written to

newItem[“Created By”] = item[“Created By”];

newItem[“Modified By”] = item[“Modified By”];

newItem[“Modified”] = item[“Modified”];

newItem[“Created”] = item[“Created”];

newItem.SystemUpdate(false);

CopyAttachments(item, newItem);

base.ItemDeleting(properties); }

Note: for the “CopyAttachments” View this Page : How to copy attachments from one list item to another

Ref : Sharepoint Tips And Tricks