3

I am using the ItemAdded event to fill in a metadata field.

In the ItemUpdated event (after submitting the file properties diagog) - I want to move the file elsewhere.

However, I get an error saying that the file was not found even though it is actually there. But I guess it's not registered as being there at this point in the sequence of events?!

My code is below:

public override void ItemUpdated(SPItemEventProperties properties)
   {
       base.ItemUpdated(properties);

       if (properties.ListTitle == "My Documents")
       {
           string fileURL = properties.ListItem.Web.Url + "/" + properties.ListItem.File.Url;
           using (SPSite siteSrc = new SPSite(fileURL))
           using (SPSite siteDst = new SPSite("http://dsidsw920"))
           using (SPWeb webSrc = siteSrc.OpenWeb())
           using (SPWeb webDst = siteDst.OpenWeb())
           {
               SPFile srcFile = properties.ListItem.File;
               SPFolder dstFolder = webDst.GetFolder("http://dsidsw920/Khurrams Documents");
               SPFile newFile = dstFolder.Files.Add(fileURL, srcFile.OpenBinary()); //Error occurs here!
           }
       }
   }

   public override void ItemAdded(SPItemEventProperties properties)
   {
       SPListItem ListItemAdded = properties.ListItem;
       if (!properties.Web.IsRootWeb)
       {
           string targetTerm = properties.Web.Name;

           if (properties.ListTitle == "My Documents")
           {
               SPWeb web = properties.Web.Site.RootWeb;

               TaxonomyField field = (TaxonomyField)properties.ListItem.Fields["WVTag"];
               Guid termSetId = field.TermSetId;

               //get term/s
               List<Term> terms = new List<Term>();
               TermStore termStore = GetTermStore(web);
               TermSet termSet = termStore.GetTermSet(termSetId);
               terms = termSet.GetAllTerms().ToList<Term>();

               bool siteNameFound = false;

               Term tagTerm = null;

               if (terms.Count > 0)
               {
                   foreach (Term term in terms)
                   {
                       if (term.Name == targetTerm)
                       {
                           tagTerm = term;
                           siteNameFound = true;
                       }
                   }
               }
               if (terms.Count == 0 || siteNameFound == false)
               {
                   tagTerm = termSet.CreateTerm(targetTerm, properties.Web.UICulture.LCID);
                   termStore.CommitAll();
               }

               TaxonomyFieldValue value = new TaxonomyFieldValue(field);
               value.Label = tagTerm.GetDefaultLabel(1033);
               value.TermGuid = tagTerm.Id.ToString();

               lock (LockObject)
               {

                   field.SetFieldValue(properties.ListItem, value);
               }
               EventFiringEnabled = false;
               properties.ListItem.Update();
               EventFiringEnabled = true;
           }
       }
       base.ItemAdded(properties);
   }

Your help appreciated thanks.

KS

0

2 Answers 2

3

Your fileUrl variable you used should be destination file url. Try this

byte[] fileBytes = sourceItem.File.OpenBinary();
bool overwritedestfile = true;
string destinationUrl = destinationList.RootFolder.Url + "/" + sourceItem.File.Name;
SPFile destinationFile = ((SPDocumentLibrary)destinationList).RootFolder.Files.Add(destinationUrl, fileBytes, overwritedestfile);
1
  • Yes, you are right doh! Now I just need to figure out how to transfer the metadata column values :S
    – Perplexed
    Apr 25, 2012 at 18:03
1

SPFile.CopyTo is a lot better to use than reading the entire file and adding it.

2
  • 1
    Unless you are moving to another sitecollection - does it work when copying from subsite doclib to root site doclib (as per my scenario)?
    – Perplexed
    Apr 25, 2012 at 18:12
  • No it's "internal" Apr 25, 2012 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.