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;
                            }
                        }
                    }

                });

Thursday, June 20, 2013

WebProvisioned web event - Feature Stapling - sharepoint 2010

WebProvisioned web event receiver -allow to add new additional logic when a site is created-is a new receiver type in SharePoint 2010. This operates at site collection level only.
<Receivers Scope="Site">
You can specify scope to "Site" or "Web" & acoordingly receiver will act SPSite.EventReceivers or SPWeb.EventReceivers

Feature Stapling -allow to add new additional logic when a site is created- will affect whole farm.

Saturday, June 15, 2013

SharePoint 2010 Property Bag - Read / Write data to Property Bag


Property Bag = hash table of Key-Value pairs

Read / Write data to Property Bag

SPSecurity.RunWithElevatedPrivileges(delegate()
        {
        try
        {
            using (SPSite RootSite = new SPSite(URL))
            {
                using (SPWeb SiteCollection = RootSite.OpenWeb())
                {                   
                    try
                    {
                        SiteCollection.AllowUnsafeUpdates = true;
                       // Get connection string from Property bag
                        if (SiteCollection.AllProperties.ContainsKey("ConnectionString"))
                        {
                            ConnectionString = SiteCollection.AllProperties["ConnectionString"].ToString();
                        }                       
                        // Set siteID in the Property bag
                        SiteCollection.Properties["siteID"] = siteID;
                        SiteCollection.Properties.Update();
                        SiteCollection.AllowUnsafeUpdates = false;                       
                    }
                    catch (Exception ex)
                    {
                      //Handle Exception 
                    }          
                }
            }
        }
        catch(Exception ex)       
        {           
        }
        }); 


http://pbs2010.codeplex.com/