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 have got custom content type

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <!-- Parent ContentType: Contract (PkbInfo.SP\Contract) (0x0101000bb17de4384349b2b6a6800bc8555513) -->
  <ContentType ID="0x0101000bb17de4384349b2b6a6800bc8555513000cd70d4f20e241dcaf9cdd46ad9d963b"
               Description="Umowa podprogowa"
    <Folder TargetName="_cts/ContractorTemplates" />
    <DocumentTemplate TargetName="/_cts/ContractorTemplates/podprogowa.dotx" />

I try do add file and set item properties

string fileName = "UDP-" + tbxDocNum.Text + ".doc";

            SPContentType cct = SpWebManager.Contracts.ContentTypes[new SPContentTypeId(ddlTempleteTypes.SelectedValue)];
            SPListItem itm = SpWebManager.Contracts.CreateDocument(_proContractsFld, cct, fileName);

            SPFile f = itm.File;
            itm = SpWebManager.Contracts.GetItemById(itm.ID);

            itm["ContentType"] = cct;
            itm["Invoice"] = new SPFieldLookupValue(_invoice.ID, _invoice.ID.ToString());
            itm["ContractorPostCode"] = tbxPostcode.Text;

where CreateDocument method is as below

public static SPListItem CreateDocument(this SPList list, SPFolder folder, SPContentType cct, string fileName, Hashtable ht)
            string sTemplate = cct.DocumentTemplateUrl;

            SPFile file = list.ParentWeb.GetFile(sTemplate);
            byte[] binFile = file.OpenBinary();

            string destFile = folder.ServerRelativeUrl + "/" + RemoveDisallowedCharsForFileName(fileName);
            SPFile addedFile = folder.Files.Add(destFile, binFile,true);

            SPListItem newItm = addedFile.ListItemAllFields;

            return newItm;    

The file is created. But when I open newly created document from library the "ContractorPostCode" is not set. On list item view (DispForm) there is value. not set fields in word field value is set at list item level How to effective create new word document and init metadata fields from related file ListItem? The user should have "ContractorPostCode" filled in word adter first editing that file.

PS. I try http://sacarter.wordpress.com/2011/11/07/add-a-file-to-a-document-library-and-update-metadata-properties-in-a-single-method/ without luck

share|improve this question
add comment

Your Answer


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

Browse other questions tagged or ask your own question.