Thursday, July 30, 2015

Convert - - StringToInt / intTostring / doubleTostring / NullToString /ConvertToDouble / NullToString

 Convert - - StringToInt / intTostring / doubleTostring / NullToString /ConvertToDouble / NullToString


   /// <summary>
        /// Gets the value.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance">The instance.</param>
        /// <returns>T.</returns>

        public static T GetValue<T>(this string instance)
        {
            T obj = default(T);
            try
            {
                obj = GenericDataConverter<T>.Convert(instance);
                return obj;
            }
            catch
            {
                throw;
            }
        }
----------------------------------------------------
        /// <summary>
        /// Converts the string to int.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>System.Int32.</returns>

        public static int ConvertStringToInt(this string value)
        {
            int retValue = 0;
            try
            {
                if (value != "" && value != null)
                {
                    retValue = Convert.ToInt32(value);
                }
                else
                {
                    retValue = 0;
                }
            }
            catch
            {
                throw;
            }
            return retValue;
        }
        /// <summary>
        /// Converts the int to string.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>System.String.</returns>
        public static string ConvertIntToString(this int value)
        {
            string retValue = string.Empty;
            try
            {
                if (value >= 0)
                {
                    retValue = Convert.ToString(value);
                }
                else
                {
                    retValue = string.Empty;
                }
            }
            catch
            {
                throw;
            }
            return retValue;
        }
 ---------------------------------------------------------------------------------------
        /// <summary>
        /// Converts the double to string.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>System.String.</returns>
        public static string ConvertDoubleToString(this double value)
        {
            string retValue = string.Empty;
            try
            {
                if (value >= 0 || value < 0)
                {
                    retValue = string.Format("{0:0.00}", value);
                    //retValue = value.ToString("0.##");
                }
                else
                {
                    retValue = string.Empty;
                }
            }
            catch
            {
                throw;
            }
            return retValue;
        }

------------------------------------------------------------------------
        /// <summary>
        /// Converts the null to string.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>System.String.</returns>
        public static string ConvertNullToString(this string value)
        {
            try
            {
                if (string.IsNullOrEmpty(value))
                {
                    value = string.Empty;
                }
            }
            catch
            {
                throw;
            }
            return value;
        }

----------------------------------------------------------------------
        /// <summary>
        /// Converts to double.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>System.Double.</returns>
        public static double ConvertToDouble(this object value)
        {
            double retValue = 0;
            try
            {
                if (value.ConvertNullToString() != "")
                {
                    retValue = Convert.ToDouble(value);
                }
                else
                {
                    retValue = 0;
                }
            }
            catch
            {
                throw;
            }
            return retValue;
        }

------------------------------------------------------------
        /// <summary>
        /// Converts the null to string.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>System.String.</returns>
        public static string ConvertNullToString(this object value)
        {
            try
            {
                if (value == null)
                {
                    value = string.Empty;
                }
            }
            catch
            {
                throw;
            }
            return value.ToString();
        }



----------------------------------------------------------------
        /// <summary>
        /// Converts to currency.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>System.String.</returns>
        public static string ConvertToCurrency(this object value)
        {
            //ToString("C", CultureInfo.CurrentCulture)
            string retValue = "$0.00";
            try
            {
                if (value.ConvertNullToString() != "")
                {
                    CultureInfo currentCulture = CultureInfo.CurrentCulture;
                    CultureInfo newCulture = new CultureInfo(currentCulture.Name);
                    newCulture.NumberFormat.CurrencyNegativePattern = 1;
                    retValue = value.ConvertToDouble().ToString("C", newCulture);
                }
                else
                {
                    retValue = "$0.00";
                }
            }
            catch
            {
                throw;
            }
            return retValue;
        }

Tuesday, July 21, 2015

Get all folders(subfolder) files in a list/Library-Report- C#

Get all folders(sub folders) files/itmes in a list/Library Report


        static void getAllWebs(String siteURL)
        {
            StreamWriter SW;
            SW = File.AppendText("C:\\POS_FilesIDandURL.csv");
            SW.WriteLine("FileURL,FileID");
            using (SPSite site = new SPSite(siteURL))
            {
                foreach (SPWeb web in site.AllWebs)
                {
                    Console.WriteLine(web.Url);
                    AllItemsInADocLibrary(web.Url, SW);
                }
            }
            SW.Close();
        }

        static void AllItemsInADocLibrary(string webURL, StreamWriter SW)
        {
            string listName = string.Empty;


            using (SPSite site = new SPSite(webURL))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    if (web.Title.ToString().Contains('_'))
                    {
                        listName = "DeptDocLibrary_" + web.Title.ToString().Split('_')[1];
                        SPList list = web.Lists.TryGetList(listName);
                        if (list != null)
                        {
                            SPView Myview = list.Views["All Documents"];

                            SPQuery oQuery = new SPQuery(Myview);
                            oQuery.ViewAttributes = "Scope=\"Recursive\"";
                            SPListItemCollection itemColl = list.GetItems(oQuery);

                            foreach (SPListItem item in itemColl)
                            {
                                SW.WriteLine(item.Url.Replace(",", " ") + "," + item.ID);
                            }
                        }
                        else
                        {
                            Console.WriteLine("List :{0} - Not found in the web : {1} ", listName, web.Title);
                        }

                    }
                }
            }

        }