Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

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);
share|improve this answer
    
Yes, you are right doh! Now I just need to figure out how to transfer the metadata column values :S –  Perplexed Apr 25 '12 at 18:03
add comment

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

share|improve this answer
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 '12 at 18:12
    
No it's "internal" –  Per Jakobsen Apr 25 '12 at 18:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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