Monday, January 27, 2014

Programmatically create folder if not exists and resize and upload image into sharepoint document library

SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite sysSite = new SPSite(SPContext.Current.Site.Url))
                    {
                        using (SPSite oSite = new SPSite(SPContext.Current.Site.Url, sysSite.SystemAccount.UserToken))
                        {
                            using (SPWeb oWeb = oSite.OpenWeb())
                            {

                                oWeb.AllowUnsafeUpdates = true;
                                if (radAsyncUpload1.UploadedFiles.Count > 0)
                                {
                                    string sFolderName = "MatrimonialThumbnail";
                                    sFileName = Convert.ToString(DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")).Replace("/", "").Replace(" ", "").Replace(":", "") + "_EIcon" +  radAsyncUpload.UploadedFiles[0].GetExtension();
                                    byte[] m_byt = new byte[radAsyncUpload1.UploadedFiles[0].ContentLength + 1];

                                    using (System.IO.FileStream fStream = (System.IO.FileStream)radAsyncUpload1.UploadedFiles[0].InputStream)
                                    {
                                        byte[] contents = new byte[fStream.Length];
                                        fStream.Read(contents, 0, (int)fStream.Length);
                                        SPList lstPublicImage = oWeb.Lists["Public"];
                                        SPFolder subFolder = null; //oWeb.Lists["Public Images"].RootFolder;

                                        bool folderExists = false;
                                        // Check to see if folder already exists, if not create it
                                        for (int i = 0; i < lstPublicImage.Folders.Count; i++)
                                        {
                                            if (string.Compare(lstPublicImage.Folders[i].Folder.Name.ToLower(), sFolderName.ToLower()
                                                , true) == 0)
                                            {
                                                subFolder = lstPublicImage.Folders[i].Folder;
                                                folderExists = true;
                                            }
                                        }
                                        if (!folderExists)
                                        {
                                            SPFolderCollection _folders = lstPublicImage.RootFolder.SubFolders;
                                            _folders.Add(oWeb.Lists["Public"].RootFolder.Url + "/" + sFolderName);
                                            lstPublicImage.Update();

                                            subFolder = lstPublicImage.Folders[lstPublicImage.Folders.Count - 1].Folder;
                                        }
                                        SPFile sps = null;
                                        // resize image
                                        System.Drawing.Image originalImage = null;
                                        originalImage = System.Drawing.Image.FromStream(fStream);
                                        if (originalImage.Width > 24 || originalImage.Height > 24)
                                        {
                                            System.Drawing.Image thumbnail = null;                                                                    
                                            MemoryStream msOut = new MemoryStream();
                                            thumbnail = originalImage.GetThumbnailImage(22, 22, null, IntPtr.Zero);
                                            thumbnail.Save(msOut, System.Drawing.Imaging.ImageFormat.Png);
                                            sps = subFolder.Files.Add(sFileName, msOut, true);
                                        }
                                        else
                                        {
                                            sps = subFolder.Files.Add(sFileName, fStream, true);
                                        }
                                        //                                       
                                        sps.Update();

                                    }
                                }
                                oWeb.AllowUnsafeUpdates = false;
                            }
                        }
                    }

                });