Wednesday, August 17, 2011

Uploading File in Sharepoint Document Library in folder

                                using (SPWeb userweb = new SPSite(url).OpenWeb())
                                {
                                    userweb.AllowUnsafeUpdates = true;
                                    //here url is nothing but site url,My Documents is Folder in document library                                    SPFolder oSPFolder = userweb.GetFolder(url + "/My Documents/");
                                    //here i have a object of file which i need to upload as SPFILE                                    SPFile file = properties.ListItem.File;
                                    if (file.ModifiedBy.LoginName == currentusr)
                                    {
                                        //get file in Byte formate for Upload bz when we say oSPFolder.Files.Add it
                                         requires Byte
                                        byte[] data = file.OpenBinary();
                                        // this will upload my file to document library in that particular folder
                                        oSPFolder.Files.Add(file.Name, data, file.Author, file.Author, file.TimeCreated, DateTime.Now);
                                    }
                                }

No comments:

Post a Comment