Search This Blog

Tuesday, December 22, 2015

SharePoint - Taxonomy -2013

In this post, I will explain how to define taxonomy through Term Store, Content Types etc in SharePoint 2013. Taxonomy is managed by Taxonomy Term Store in SharePoint 2013 where we can create Terms(a Term is a specific word or phrase that you associated with an item on a SharePoint site), Term Sets(a Term Set is a group of related terms) and Groups(a group is a set of term sets).

Lets walk through following steps:
  1. Configure Terms in Term Store
  2. Create Content Type with Managed Metadata
  3. Add Document and Tag
  4. Metadata Navigation 
 1 - Configure Terms in Term Store
  1. Go to Central Admin and select Managed Metadata Service to launch the Term Store.
  2. Provide your ID in Term Store Administrators and hit Save in order to have the right permissions to create Terms.
  3. Select the tiny arrow on Home node (Taxonomy_XXX) and create a new group called Function.
  4. Create your Term Set (Advisory) and Terms (Business, IT, KM) under Function group.

    Note: You can provide description of groups, term sets and terms on the right side.
 2 - Create Content Type with Managed Metadata
  1. Launch the Site Collection where you want to use these terms and go to the Site Settings -> Site Columns.
  2. Create a site column Advisory and type Managed Metadata
  3. Under Term Set Settings expand taxonomy and select Advisory term set and hit OK.

    Note: This will map Advisory term set to the Advisory site column.
  4. Create a new Site Content Type Function that will use the Advisory site column
  5. Add the Function content type to a Document Library

    Note: Now the library is ready to use.
 3 - Add Document and Tag
  1. Upload a document in Document Library and select Function content type.
  2. In Advisory column select a the Term that you want to associate with this document and save.
  3. Add a couple of more documents with different terms
  4. And that's how you attach Taxonomy Terms (Managed Attributes) with different documents in SharePoint in order to categorize them.
 4 - Metadata Navigation 
  1. Go to Library Settings of MyDocuments and select Metadata navigation settings.

    Note: You might have to activate Metadata Field Navigation and Filtering feature from Site Features if this option is not available. 
  2. In Metadata Navigation Settings select Advisory as Hierarchy Field.
  3. Go back to the library and Advisory Term Set will appear on the left navigation
  4.  Select a Term from the navigation and you will see related documents appearing on left.
And that's how taxonomy is implemented in SharePoint 2013.

Ref: http://office.microsoft.com/en-us/sharepoint-help/introduction-to-m...

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