locked
How to rename a file without creating a new version ? RRS feed

  • Question

  • We are using Sharepoint foundation 2010. We want to upload document to document library and at the same time it should be renamed. Means it should not create one version while uploading document and one version while renaming file. We are using following code

    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        // 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)
                            {
                                //SPContentType ctype = web.ContentTypes["Document"];
                                //Get list with specified name in the existing web site
                                SPList list = web.Lists[libraryName];
                                string newFileName = "BV Question3";
                                //RenameFile(documentGuid, siteUrl, libraryName, newFileName);
                                //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
                                // Set the Content Type and Metadata for the Document prior to adding the File      
                                Hashtable properties = new Hashtable();
                                //properties.Add("ContentTypeId", ctype.Id.ToString());      
                                //properties.Add("ContentTypeId", ctype.Id.ToString());   0x0101   
                                //properties.Add("ContentTypeId", ctype.Id.ToString());
                                properties.Add("User Name", userName);
                                properties.Add("Document Type", documentType);
                                properties.Add("Check Type", checkType);
                                var overWrite = true;
                                
                                SPFile file = folder.Files.Add(saveFileWithName, fileContents, properties, overWrite);
    }
    }
    });

    My file gets successfully uploded. Once the file gets uploaded it creates new version. That is fine for me. Then I want to rename the file. When I rename it it creates one another new version. I want code through which these both task will happen but it will create only one version. Can you please provide me any code or link through which I can resolve the above issue ?
    Tuesday, June 25, 2013 6:44 AM

Answers

  • Hi

    Use litem.SystemUpdate(); will update the list item without changing the Modified or Modified By fields and version.

    SPListItem.SystemUpdate();

    Please mark the replies as answers if they help or unmark if not.

    Tuesday, June 25, 2013 7:01 AM