Search This Blog

Monday, December 21, 2015

Reading Data From Text and Folder Image Based On Text Data and Insert into Library

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.ServiceModel.Activation;   
using Microsoft.Office.Server.Social; 
using Microsoft.Office.Server.UserProfiles; 
using Microsoft.Office.Server.Microfeed; 
using Microsoft.SharePoint; 
using Microsoft.Office.Server.ReputationModel; 
using Microsoft.Office.Server.SocialData;   
using System.Configuration;   
using System.Security.Principal; 
using System.Web; 
using Microsoft.Web.Hosting.Administration;
using System.IO;
using System.Collections;
using System.Drawing;
 
//http://0179nitiv002tvs:21287/_layouts/15/SharePoint.WCFService.Sample/Services/SampleService.svc/ImgUploadFolder(Robin%20Sharma)
namespace SharePoint.WCFService.Sample.Code 
{  
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]  
    public sealed class SampleService : ISampleService  
    {
        string[] linecoll = null;
        string[] txtimglinecoll = null;
        string previousTitle = null;
    
        public string ImgUploadFolder(string sharedPath)  
        {
          try
            {
               string finalpath = Encoding.UTF8.GetString(Convert.FromBase64String(sharedPath));
               if (Directory.Exists(finalpath))
                {
                    //string finalpathinput = "@" +  finalpath ;
                  string[] array1 = Directory.GetFiles(@"E:\RS\");
                   // string[] array1 = Directory.GetFiles(finalpathinput);
                 // string []array2=Directory.GetFiles(@"E:\RS\Images\","*.png");
                  string line = "";
                  try
                  {
                      StreamReader file = new StreamReader(array1[0]);
                      line = file.ReadToEnd();
                      linecoll = line.Split(';');
                      for (var i = 0; i <= linecoll.Length - 2; i++)
                      {
                          string[] totlline = linecoll[i].Split('#');
                          InsertDetails(totlline[0].ToString(), totlline[1].ToString(), totlline[2].ToString(), totlline[3].ToString(), totlline[4].ToString());
                          InsertDetailsWithImages();
                      }
                 }
                  catch (FileNotFoundException ex)
                  {
                      return ex.Message;
                  }
                    return "success";
                }
                return "File not exists " + finalpath;  
                //return File.ReadAllText(@"E:\" + sharedPath + @"\info.txt");
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
       
        public void InsertDetails(string eventtitle,string companynam,string companyunit,stringcreateddate,string epd)
        {
           string []LastTitlePostId = GetLastInsertID().Split(',');
           if(LastTitlePostId!=null)
           {
               previousTitle=LastTitlePostId[1];
           }
           if (previousTitle != null)
           {
               if (eventtitle == previousTitle)
               {
               }
               else
               {
                   try
                   {
                       using (SPSite oSPsite = new SPSite(SPContext.Current.Site.Url))
                       {
                           using (SPWeb oSPWeb = oSPsite.OpenWeb())
                           {
                               oSPWeb.AllowUnsafeUpdates = true;
                               SPList list = oSPWeb.Lists["db_Awards"];
                               SPListItem itemToAdd = null;
 
                               itemToAdd = list.Items.Add();
                               itemToAdd["Title"] = eventtitle;
                               itemToAdd["Body"] = companynam;
                               itemToAdd["HeadlineText"] = companyunit;
                               itemToAdd.Update();
                               list.Update();
                               oSPWeb.AllowUnsafeUpdates = false;
 
                           }
                       }
                   }
                   catch (Exception ex)
                   {
 
                   }
               }
           }
            
        }
 
        public void InsertDetailsWithImages()
        {
          string imgcontent = "";
          string []Inserted = GetLastInsertID().Split(',');
          if(Inserted!=null)
            {
           string LastId= Inserted[0] ;
           string totllineimglineMatch = "";
            try
            {
                using (SPSite oSPsite = new SPSite(SPContext.Current.Site.Url))
                {
                    using (SPWeb oSPWeb = oSPsite.OpenWeb())
                    {
                        oSPWeb.AllowUnsafeUpdates = true;
                        SPList listadd = oSPWeb.Lists["pl_Awards"];
                        SPFileCollection collFilesDest = listadd.RootFolder.Files;
                     
                        string[] array2 = Directory.GetFiles(@"E:\RS\Images\");
                        for (var i = 0; i <= array2.Length - 1; i++)
                        {
                            if (array2[i].IndexOf(".txt") != -1)
                            {
                                StreamReader Imagedeatils = new StreamReader(array2[i]);
                                imgcontent = Imagedeatils.ReadToEnd();
                                txtimglinecoll = imgcontent.Trim().Split(';');
                            }
                        }
                        DirectoryInfo df = new DirectoryInfo(@"E:\RS\Images\");
                        string multipleExtensions = "*.jpg;*.gif;*.png;*.jpeg";
                        string[] extensions = multipleExtensions.Split(new char[] { ';' });
                        List<FileInfo> fileInfos = new List<FileInfo>();
                        foreach (string extension in extensions)
                        {
                            fileInfos.AddRange(df.GetFiles(extension));
                        }
                        FileInfo[] oFileList = fileInfos.ToArray();
                        for (var i = 0; i <= txtimglinecoll.Length -2; i++)
                        {
                            string[] totllineimgline = txtimglinecoll[i].Split('#');
                         
                            if (totllineimgline[0].ToString().IndexOf("\r\n") != -1)
                            {
                                totllineimglineMatch = totllineimgline[0].ToString().Remove(0, 2);
                            }
                            else
                            {
                                totllineimglineMatch = totllineimgline[0].ToString();
                            }
 
                            foreach (FileInfo fi in oFileList)
                                {
 
                                    string fileName = fi.Name;
                                    fileName = fileName.Remove(fileName.Length - 4);
                                    byte[] imageData = null;
                                    imageData = System.IO.File.ReadAllBytes(fi.FullName);
                                    if (fileName == totllineimglineMatch)
                                      {
                                         using (SPSite site = newMicrosoft.SharePoint.SPSite(SPContext.Current.Site.Url))
                                          {
                                              using (SPWeb web = site.OpenWeb())
                                              {
                                                  web.AllowUnsafeUpdates = true;
                                                  bool IsOverwriteFile = false;
                                                  SPList picslibrary = web.Lists["pl_Awards"];
                                                  SPFile file = picslibrary.RootFolder.Files.Add(fi.Name, imageData, IsOverwriteFile);
                                                  SPListItem item = file.Item;
                                                  item["Description"] = totllineimgline[1].ToString();
                                                  item["PostID"] = LastId;
                                                  item.Update();
                                                  file.Update();
                                                  web.AllowUnsafeUpdates = false;
 
                                              }
                                          }
                                    }
 
                               
                                }
                           
                            
                        }
      
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }          
            }
 
                                                                                                
         
 
 
 
        }
        public static string GetLastInsertID()
        {
         
            string lastId = "";
            string lastinsertIdentity = "";
            string priviousTitle = "";
            using (SPSite site = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                
                    SPList list = web.Lists["db_Awards"];
                    SPQuery query = new SPQuery();
                    query.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
                    query.RowLimit = 1;
                    SPListItemCollection items = list.GetItems(query);
                    if (items.Count > 0)
                    {
                        SPListItem _item = items[0];
                         lastId= _item["ID"].ToString();
                         priviousTitle = _item["Title"].ToString();
                    }
                }
            }
            lastinsertIdentity = lastId + "," + priviousTitle;
            return lastinsertIdentity;
        }
       
        }
    } 
 
 

No comments:

Post a Comment