Showing posts with label Sharepoint Object Model. Show all posts
Showing posts with label Sharepoint Object Model. Show all posts

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

Thursday, 24 April 2014

Modify SharePoint web.config using object model....

Root Level Web.Config

  <appSettings>
    <
add key="AppKey" value="AppValue"/>  </appSettings>

Code Snippet

using System;using System.Configuration;using System.Web.Configuration;
namespace SampleApplication.WebConfig
{
    public partial class webConfigFile : System.Web.UI.Page    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Helps to open the Root level web.config file.            Configuration webConfigApp = WebConfigurationManager.OpenWebConfiguration("~");
            //Modifying the AppKey from AppValue to AppValue1            webConfigApp.AppSettings.Settings["AppKey"].Value = "AppValue1";
            //Save the Modified settings of AppSettings.            webConfigApp.Save();        }
    }
}

Output of Root Level Web.Config:

 <appSettings>
    <
add key="AppKey" value="AppValue1"/>  </appSettings>

In order to modify application specific web.config entry below is the code snippet:



using System;
using System.Reflection;
using Microsoft.SharePoint.Administration;
 
namespace SharePointLiveForMe.SharePoint.Administration
{
    /// <summary>
    /// This class allows to add or remove entries in web.config.
    /// </summary>
    public class WebConfigManagement
    {
        /// <summary>
        /// This methods allows to add a key/value pair in the AppSettings section.
        /// </summary>
        /// <param name="Key">Name of the key</param>
        /// <param name="Value">Value</param>
        /// <param name="webApp">Web application</param>
        public static void AddAppSettingsKeyValue(string key, string value, SPWebApplication webApp)
        {
            if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value) && webApp != null)
            {
                SPWebConfigModification modification = GetModification(key, value);
                //Add app setting key value if it is not there in web.config file 
                if (!webApp.WebConfigModifications.Contains(modification))
                {
                    webApp.WebConfigModifications.Add(modification);
                    webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
                    webApp.Update();
                }
                else
                {
                    //modify existing entry
                    webApp.WebConfigModifications.Add(modification);
                    webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); 
                    webApp.Update();
                } 
            }
            else
                throw new ArgumentNullException();
        }
 
        /// <summary>
        /// This methods allows to remove a key/value pair in the AppSettings section.
        /// </summary>
        /// <param name="key">Name of the key</param>
        /// <param name="value">Value</param>
        /// <param name="webApp">Web application</param>
        public static void RemoveAppSettingsKeyValue(string key, string value, SPWebApplication webApp)
        {
            if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value) && webApp != null)
            {
                SPWebConfigModification modification = GetModification(key, value);
                if(webApp.WebConfigModifications.Contains(modification))
                {
                    webApp.WebConfigModifications.Remove(modification);
                    webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
                    webApp.Update();
                }
            }
            else
                throw new ArgumentNullException();
        }
 
        /// <summary>
        /// This method allows to build an SPWebConfigModification object.
        /// </summary>
        /// <param name="key">Name of the key</param>
        /// <param name="value">Value</param>
        /// <returns></returns>
        private static SPWebConfigModification GetModification(string key, string value)
        {
            if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
            {
                SPWebConfigModification modification = new SPWebConfigModification();
                modification.Name = string.Format(@"add[@key=""{0}""]", key);
                modification.Path = "configuration/appSettings";
                modification.Value = string.Format(@"<add key=""{0}"" value=""{1}"" />", key, value);
                modification.Owner = Assembly.GetExecutingAssembly().FullName;
                modification.Sequence = 0;
                modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                return modification;
            }
            else
                throw new ArgumentNullException();
        }
    }
}

Wednesday, 23 April 2014

Read sharepoint web.config using object model...


We need to initialize the necessary variables for ex as below

           //Initialize the variables
            string configFilePath = string.Empty;
            XmlDocument webConfigDoc = null;
            string appSettingVal = string.Empty;
            string xpath = string.Empty;

           configFilePath =  rootWeb.Site.WebApplication.GetIisSettingsWithFallback(Microsoft.SharePoint.Administration.SPUrlZone.Default).Path.FullName.ToString() + "\\web.config";
           webConfigDoc = new XmlDocument();
           webConfigDoc.Load(configFilePath);

           //Reading web.config attributes
           xpath = "configuration/appSettings/add[@key='" + <key value that you are looking for> + "']";
           XmlNode appSetting = webConfigDoc.SelectSingleNode(xpath);

            if (appSetting != null)
                    {
                       //gets the value for the key from appsettings section.
                        appSettingVal = appSetting.Attributes["value"].Value;
                    }