Tuesday, October 6, 2015

Upload file to a Document Library with File metadata

Upload file to a Document Library with  File metadata




  public SPListItem UploadFile(SPList listName)
        {
            SPListItem fileItem = null;
            try
            {
                SPWeb web = listName.ParentWeb;

                SPFolderCollection sfc = web.Folders;
                string path = web.Url + "/" + listName.Title.ToString(); // +"/" + fileUpload1.FileName.ToString().Split('.')[0];
                sfc.Add(path);

                SPFile sfile = web.Files.Add(path + "/" + fileUpload1.FileName.ToString() + System.IO.Path.GetExtension(fileUpload1.PostedFile.FileName),
                fileUpload1.FileContent, true);
                listName.Update();
                fileItem = sfile.Item;
            }
            catch (Exception ex)
            {
                Message(lblerror, ex.Message);
            }

            return fileItem; // sfile.Item;

        }

AttachFile To SharePoint ListItem

AttachFile To SharePoint ListItem

  public void AttachFileToItem(SPListItem listItem)
        {
            SPAttachmentCollection attachmentCollection = listItem.Attachments;

            Stream attachmentStream;
            Byte[] attachmentContent;

            // Get the file from the fileupload control
            if (fileUplaod1.HasFile)
            {
                attachmentStream = fileUplaod1.PostedFile.InputStream;

                attachmentContent = new Byte[attachmentStream.Length];

                attachmentStream.Read(attachmentContent, 0, (int)attachmentStream.Length);

                attachmentStream.Close();
                attachmentStream.Dispose();

                // Add the file to the attachment collection
                attachmentCollection.Add(fileUplaod1.FileName, attachmentContent);
            }

            // Update the list item- SystemUpdate will not create a version
            listItem.SystemUpdate();
       
        }
    }

Monday, October 5, 2015

SharePoint Item Update: Exception Handling when Item is null or Empty

SharePoint Item Update: Exception Handling when Item is null or Empty


item["ProgramTitle"] = (wpItem["ProgramTitle"] != null) ? wpItem["ProgramTitle"].ToString() : string.Empty;

Thursday, September 24, 2015

Retrieve SharePoint Log with Correlation ID using PowerShell

Retrieve SharePoint Log with Correlation ID using PowerShell 


get-splogevent | ?{$_.Correlation -eq "CorrellationIDHer"} | select Area, Category, Level, EventID,Message | Format-List > C:\myErrorDetail.log

Wednesday, September 2, 2015

Download WSP from SharePoint CA-Solution Management- Power Shell

Download WSP from CA-Solution Management


$farm = Get-SPFarm
$file = $farm.Solutions.Item("MyProject.wsp").SolutionFile
$file.SaveAs("c:\MyProject.wsp")

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

                    }
                }
            }

        }