Tuesday 22 September 2015

Updates Title with File names in a Document Library including all subfolders

Code to accomplish .

  # Inputs for web url and document library name
            string strSiteCollURL = txtSiteCollURL.Text.ToString();
            string strDocLibr = txtDocLib.Text.ToString();
            try
            {
                SPSite site = new SPSite(strSiteCollURL);
                SPWeb web = site.OpenWeb();
                web.AllowUnsafeUpdates = true;

                SPFolder listobj = web.GetFolder(strDocLibr);
                SPDocumentLibrary libr = listobj.DocumentLibrary;
                SPQuery query = new SPQuery();

                query.ViewAttributes = "Scope=\"Recursive\"";

                SPListItemCollection listcoll = libr.GetItems(query);


                foreach (SPListItem item in listcoll)
                {
                    string eXtension = Path.GetExtension(item.Name);
                    string fileName = item.Name;

                    if (fileName != "")
                    {
                        if (eXtension != null && eXtension != "")
                        {
                            fileName = fileName.Replace(eXtension, "");
                        }
                        item["Title"] = fileName;
                    }
                    item.SystemUpdate();
                }

                System.Windows.Forms.MessageBox.Show("Successfully Updated the Titles");
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

No comments:

Post a Comment