Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am upload document to document library along with metadata using server object model in sharepoint foundation 2010. I am using following code

// Initialize instance of existing site collection
                    using (SPSite site = new SPSite(siteUrl))
                    {
                        //Initailize instance of exiting web site (for e.g. Team Site)
                        using (SPWeb web = site.RootWeb)
                        {
                            //Get list with specified name in the existing web site
                            SPList list = web.Lists[libraryName];

                            //Get the collection of folders in the existing web site
                            String url = list.RootFolder.ServerRelativeUrl.ToString();
                            SPFolderCollection folders = web.GetFolder(url).SubFolders;

                            //Add new folder in the exiting collection of folders
                            SPFolder folder = folders.Add(folderName);

                            //SPFolder folder = web.GetFolder(siteUrl + "/" + libraryName + "/" + folderName);
                            //byte[] fileContents = System.IO.File.ReadAllBytes(@fileUrl);                                                     

                            //Add file in the newly added folder with overwrite
                            var overWrite = true;
                            SPFile file = folder.Files.Add(saveFileWithName, fileContents, overWrite);


                            //Get the list item of the newly added file
                            SPListItem listItem = file.ListItemAllFields;
                            //Assign values to the fields of newly added file
                            listItem["User_x0020_Name"] = userName;
                            listItem["Document_x0020_Type"] = documentType;
                            listItem["Check_x0020_Type"] = checkType;
                            //Update the list item with the newly added values to the fields
                            listItem.Update();

                            //Get the unique id of the newly added list item
                            documentGuid = listItem["UniqueId"].ToString();

                        }
                    }

The above code works fine. I have versioning enabled on my document library. When the above code run it creates two versions in document library. One when it upload document with

SPFile file = folder.Files.Add(saveFileWithName, fileContents, overWrite);

and another when it adds values to the column User_x0020_Name, Document_x0020_Type and

Check_x0020_Type using

listItem.Update();

I want to create only only one version when user upload document as well as add metadata. How to do this ? Can you please provide me any code or link through which I can resolve the above issue ?

share|improve this question

1 Answer 1

Keep the code same just replace "listItem.Update();" with "listItem.SystemUpdate(false);" it will update the metadata and will not increase the version number.

share|improve this answer

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.