0

I have a document library with a custom column. I want to add a file programatically, as well as a value for the custom column(lookup column).

 
SPFile file = sourceListItem.ParentList.ParentWeb.GetFile(sourceListItem.Attachments.UrlPrefix + fileName);
                byte[] imageData = file.OpenBinary();
                SPFile newFile = files.Add(destinatioList.RootFolder.Url + "/" + file.Name, imageData, true);
                Hashtable targetItem = newFile.Item.Properties;
                //SPListItem targetItem = destinatioList.AddItem();
                targetItem["Venue Name"] = sourceListItem["Venu Name"].ToString();
                newFile.Update();

It does add the file to the document library, but it does not add the value for the custom column. What am I doing wrong?

1
  • Can you make sure the column names are correct for Target and Source? Plus also try using Internal names of the columns, i.e. Venue_x0020_Name instead of Venue Name and Venu_x0020_Name instead of Venu Name Commented May 21, 2014 at 23:29

3 Answers 3

1

i have two list -custom list (Venues)(has an attachment file ) - pic library (Venue Gallery)(has lookup field) i try to enter the attachment with the field name (venu name) in the pic library


base.ItemAdded(properties);
            SPListItem sourceListItem = properties.ListItem;
            SPDocumentLibrary destinatioList = (SPDocumentLibrary)sourceListItem.Web.Lists["Venue Gallery"];
            SPFileCollection files = destinatioList.RootFolder.Files;
foreach (string fileName in sourceListItem.Attachments)
            {
                SPFile file = sourceListItem.ParentList.ParentWeb.GetFile(sourceListItem.Attachments.UrlPrefix + fileName);
                byte[] imageData = file.OpenBinary();
                SPFile newFile = files.Add(destinatioList.RootFolder.Url + "/" + file.Name, imageData, true);
                SPListItem targetItem = newFile.Item;
                //Hashtable targetItem = newFile.Item.Properties;
                //SPListItem targetItem = destinatioList.AddItem();
                targetItem["Venue_x0020_Name"] = sourceListItem["Venu_x0020_Name"].ToString();
                targetItem.Update();

} destinatioList.Update();

this is my code the sourceListItem is correct but i cant find the target property to but the text on it

0

You need to set metadata on the item directly and then update the item, not the file. Try this:

SPFile file = sourceListItem.ParentList.ParentWeb.GetFile(sourceListItem.Attachments.UrlPrefix + fileName);
byte[] imageData = file.OpenBinary();
SPFile newFile = files.Add(destinatioList.RootFolder.Url + "/" + file.Name, imageData, true);
SPListItem targetItem = newFile.Item;
targetItem["Venue Name"] = sourceListItem["Venu Name"].ToString();
targetItem.Update();
4
  • still not work when i make attache processes in target item i cant find my new field Commented May 21, 2014 at 23:19
  • I guess I need better context on how/where you are using this code, as the approach I outlined does work. Commented May 21, 2014 at 23:22
  • i have two list -custom list (venus) Commented May 22, 2014 at 12:40
  • i answered your question Commented May 22, 2014 at 12:49
0
 String fileName = Path.GetFileName(_web.Site.RootWeb.Url + FileUrl);
 byte[] fileStream = _web.GetFile(lblUrl.Text).OpenBinary();// File.OpenRead(_web.Site.RootWeb.Url + lblUrl.Text);
 byte[] content = new byte[fileStream.Length];
 SPFolder myLibrary = _web.Folders[DocumentLib];
 myLibrary.Files.Add(fileName, content, true);
 myLibrary.Update();
 SPFile file = _web.GetFile(myLibrary.Url + "/" + fileName);
 int itemid = file.Item.ID;
 SPListItem splistItem = destinatioList.GetItemById(itemid);
 splistItem["Field1"] = Field1;
 splistItem["Field2"] = Field2;
 splistItem["Field3"] = Field3;
 splistItem.Update();

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.