Question

I'm copying documents between document libraries in different site collections. I have saved my source library template, uploaded to destination site collection and created library based on that template. Library has managed metadata field.

After copying file from source to destination library, I found out that my managed metadata field is not copied well. When I'm in the main view of destination library, I can't see managed metadata field values. But when I go to item edit mode, I can see proper value of that field. What is more interesting, when I upload document manually to destination library, choose metadata and save item - I can't see this field value too!

After some debugging I found out that my field after copying between libraries (called Subject) is set as follows:

Subject = |06c40660-e8e8-4580-a611-0d5c66999730
Subject_0 = Biologia|06c40660-e8e8-4580-a611-0d5c66999730

but Taxonomy Catch All Column is empty...

I'm using this code for copying and setting metadata:

using (SPSite sourceSite = new SPSite(currentSiteUrl))
                {
                    using (SPWeb sourceWeb = sourceSite.OpenWeb())
                    {
                        SPList sourceList = sourceWeb.Lists["Dokumenty"];
                        SPListItem sourceItem = sourceList.GetItemById(Int32.Parse(itemId));
                        SPFile sourceFile = sourceItem.File;
                        using (SPSite destSite = new SPSite(destinationSiteUrl))
                        {
                            using (SPWeb destWeb = destSite.OpenWeb())
                            {
                                destWeb.AllowUnsafeUpdates = true;
                                SPList destList = destWeb.Lists["Destination library name"];
                                SPFolder destFolder = destList.RootFolder;
                                SPFile destFile = destFolder.Files.Add(sourceFile.Name, sourceFile.OpenBinary(), true);

                                SPListItem destItem = destFile.Item;

                                // source taxonomy fields
                                TaxonomyFieldValue taxSourceSubject = (sourceItem["Subject"] as TaxonomyFieldValue);
                                TaxonomyField taxTargetFieldSubject = (TaxonomyField)destItem.Fields.GetFieldByInternalName("Subject");    
                                TaxonomySession taxonomySession = new TaxonomySession(destSite);
                                TermStore termStore = taxonomySession.TermStores[taxTargetFieldSubject.SspId];

                                var termSubjectSet = termStore.GetTermSet(taxTargetFieldSubject.TermSetId);
                                var termSubject = termSubjectSet.GetTerm(new Guid(taxSourceSubject.TermGuid));

                                taxTargetFieldSubject.SetFieldValue(destItem,termSubject);

                                destItem["Title"] = sourceItem["Title"];

                                destItem.Update();
                                destFile.Update();
                                string fields = "";
                                foreach (SPField f in destItem.Fields)
                                {
                                    fields += "<br/>" + f.Title + " = " + destItem[f.Title];
                                }
                                StatusLabel.Text += "<br/><br/>" + fields;
                                destWeb.AllowUnsafeUpdates = false;
                            }
                        }
                    }
                }

Maybe my destination library is missing something?

Était-ce utile?

La solution

As it showed out, the problem was that managed metadata fields in destination library come from template. After deleting metadata columns from library, and adding them back by hand (with the same name) solved the issue.

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top