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 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.

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
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.