Question

I am creating new document in document library using this code:

private SPListItem CreateNewDocument()
{
    SPContentType documentContentType;   
    byte[] bytes;
    string templatePath;
    SPFile template;
    SPFolder folder;
    string documentFileName;
    string documentPath;
    SPFile document;

    documentContentType = this.workflowProperties.Web.ContentTypes[this.contentType];

    bytes = new byte[0];
    if (!string.IsNullOrEmpty(documentContentType.DocumentTemplateUrl))
    {
        templatePath = documentContentType.DocumentTemplateUrl;
        template = this.workflowProperties.Web.GetFile(templatePath);
        bytes = template.OpenBinary();
    }

    folder = this.workflowProperties.Web.Folders[this.documentLibraryListTitle];
    documentFileName = string.Format(Constants.msWordDocumentFileNameFormat, this.documentTitle);
    documentPath = Path.Combine(folder.Url, documentFileName);
    document = this.workflowProperties.Web.Files.Add(documentPath, bytes, true);

    return document.Item;
}

Then in the next method, I am assigning property values to the document:

private void SetDocumentProperties(SPListItem item)
{
    SPUser author;

    author = workflowProperties.Web.AllUsers[this.documentAutor];

    item[SPBuiltInFieldId.Title] = this.documentTitle;
    item[SPBuiltInFieldId.ContentTypeId] = this.workflowProperties.Web.ContentTypes[this.contentType].Id;
    item[Constants.documentAuthorField] = author;
    item.UpdateOverwriteVersion();
}

All this works well and the document gets created with all properties filled properly. However, when I open the file manually, edit it in MS Word and then save it back to the document library, the Title field is blank. I would appreciate if someone could explain why is this happening and how to fix it? Thanks.

Was it helpful?

Solution

Solved this one. It appears that the DOCX documents cannot be of size zero and that they need at least a MainPart in order not to be corrupt. Having the proper DOCX file solves the issue with the missing title property.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top