SharePoint 2010: Extension Methods


SharePoint 2010 : SPFileCollection , SPListItemCollection  and SPFolderCollection Extension Methods

List  of Extension Methods
  1. SPFileCollection
  2. SPListItemCollection
  3. SPFolderCollection   


    {
        internal static List<SPFileInfo> ToFileNodeInfo(this SPFileCollection files)
        {
            List<SPFileInfo> nodeInfos = new List<SPFileInfo>();

            foreach (SPFile file in files)
            {
                SPFileInfo nodeInfo = new SPFileInfo
                {
                    Id = file.Item.ID,
                    Name = file.Name,
                    UniqueId = file.Item.UniqueId,
                    FileType = file.Item[SPBuiltInFieldId.File_x0020_Type] as string,
                    ServerRelativeUrl = file.ServerRelativeUrl,
                    Title = file.Item.Title,
                    IsCheckedOut = file.Level == SPFileLevel.Checkout
                };
                nodeInfos.Add(nodeInfo);
            }

            return nodeInfos;
        }
       
       internal static List<SPFolderInfo> ToFolderNodeInfo(this SPFolderCollection folders)
            {
                List<SPFolderInfo> nodeInfos = new List<SPFolderInfo>();

                foreach (SPFolder folder in folders)
                {
                    SPFolderInfo nodeInfo = new SPFolderInfo
                    {
                        Name = folder.Name,
                        Url = folder.Url
                    };
                    nodeInfos.Add(nodeInfo);
                }

                return nodeInfos;
            }

       internal static List<SPFileInfo> ToFileNodeInfo(this SPListItemCollection listItems)
       {
           List<SPFileInfo> nodeInfos = new List<SPFileInfo>();

           foreach (SPListItem listItem in listItems)
           {
               if (listItem.File != null)
               {
                   SPFileInfo nodeInfo = new SPFileInfo();

                   nodeInfo.Id = listItem.ID;
                   nodeInfo.Name = listItem.File.Name;
                   nodeInfo.UniqueId = listItem.UniqueId;
                   nodeInfo.FileType = listItem[SPBuiltInFieldId.File_x0020_Type] as String;
                   nodeInfo.ServerRelativeUrl = listItem.File.ServerRelativeUrl;
                   try
                   {
                       nodeInfo.Title = listItem.Title;
                   }
                   catch (ArgumentException)
                   {
                       nodeInfo.Title = listItem.File.Title;
                   }
                   nodeInfo.IsCheckedOut = listItem.File.Level == SPFileLevel.Checkout;

                   nodeInfos.Add(nodeInfo);
               }
           }

           return nodeInfos;
       }
    }


    [Serializable]
    public class SPFileInfo
    {
        #region Properties

        /// <summary>
        /// Gets or sets the Id.
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// Gets or sets the unique Id.
        /// </summary>
        public Guid UniqueId { get; set; }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the file type.
        /// </summary>
        public string FileType { get; set; }

        /// <summary>
        /// Gets or sets the server relative url.
        /// </summary>
        public string ServerRelativeUrl { get; set; }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// Gets or sets the is checked out flag.
        /// </summary>
        public bool IsCheckedOut { get; set; }

        /// <summary>
        /// Gets or sets the file contents which is used to save the contents to SharePoint.
        /// </summary>
        public string Contents { get; set; }

        #endregion
    }

    public class SPFolderInfo
    {
        #region Properties

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the url.
        /// </summary>
        public string Url { get; set; }

        #endregion
    }



Comments

Popular posts from this blog

SharePoint RPC Protocols Examples Using OWSSVR.DLL

Send Email using SharePoint Rest API

Query suggestions in SP 2013 Using Rest API (/_api/search/suggest)