views:

278

answers:

3
  1. I need to copy the item that user just added (for example. myresume.doc or financial.xls) with the metadata (doc lib obtains the columns from content type, ct obtains the columns from site colum) and copy the item with metadata in a folder called "NativeFile". Every doc library has this folder.

  2. I know itemadded can be used but then I heard itemadded fires before user have a chance to complete the metadata for the item they just added.

What are my options? (new to sp, so some sample code would greatly help. or some good link similar to this issue)

Sharepoint 2007, itemadded or itemadding or itemupdating or itemupdated....

A: 

What you've heard is correct: ItemAdded will fire before metadata completion because in a document library, ItemAdded fires when the document is first uploaded and not when the form is filled out.

I think the smoothest option is to use a workflow that fires off when an item is created in the document library. Specifically a code-based one using Visual Studio 2008, if you have it. A general walkthrough of making workflows using Visual Studio 2008 can be found here: Wrangling SharePoint Workflows with Visual Studio. That article is actually how I learned to make workflows from in the first place, so I hope it helps you as well as it has me. I would recommend this approach over an event handler because you would have to use ItemUpdated, which means you'd have to do tricky checks just to ensure this only runs once. And the other option I can think of, which is making a custom form, is not very comfortable to have to do all of the time.

Now, make your own workflow. Make sure to initialize the workflowProperties like shown in the link for the onWorkflowActivated_Invoked. This is a very simple workflow if you make all of the necessary fields Required in the form. In designer view, all you need are 2 additional activities besides the OnWorkflowActivated activity: an OnWorkflowItemChanged activity, and a Code activity. For the OnWorkflowItemChanged activity, the only property you need to set is the CorrelationToken, which will be the same as the OnWorkflowActivated activity's CorrelationToken. Now, double click the Code activity to generate its codeActivity_Executed method. In this method, the following should suffice:

SPListItem item = workflowProperties.Item;

if (!item.File.Url.Contains("/NativeUrl/"))

{

string destination = item.ParentList.RootFolder.Name + "/NativeFile/" + item.File.Name;

item.File.CopyTo(destination, true);

}

I added the if statement because the workflow will probably trigger on the new item you created by using CopyTo, so this will check to see if the file is already in the folder, and if it is, it won't do a copy. Now, associate the workflow with your website like demonstrated in the link, and associate it with item creation. It should work, then.

Even though this workflow techinically waits for ItemUpdated, because that is when the fields are filled out, by associating it with creating an item, you ensure that it only runs once. I do admit that I've only used item.File.MoveTo(destination, true), but that does retain all of the fields and metadata, so I'm pretty sure the CopyTo should as well. If not, just give a holler.

ccomet
A: 

ccomet is correct. With item event receivers alone there's no good way to duplicate the metadata. If you can get away with just copying the file here's whatcha do:

public class MyItemEventReceiver : SPItemEventReceiver
{
    public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);

        SPListItem item = properties.ListItem;
        item.File.CopyTo(item.ParentList.RootFolder.Name + "/NativeFile/" + item.File.Name, true);
    }
}
Repo Man
A: 

Just copying the Properties collection from a source item to a destination item doesn't work for all file types, especially when they are located in different document libraries.

See this thread for more details: http://www.sharepointoverflow.com/questions/1151/what-is-the-best-way-to-copy-a-files-meta-data-in-sharepoint

Muhimbi