0

here am trying to create one sahrepoint reusable control for which i need to pass some data which suppose to be in .config file, e.g here am sending mail from sharepoint control and i need to pass user name and password to my code so for this in simple asp.net/MVC project we are putting our data into web.config file and usiing that data in my mail sending code,so what if i want to do the same in sharepoint reusable control??
here in sharepoint solution am adding one app.config file and in that file am putting user name and password like

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <appSettings>
    <add key="PFUserName" value="abc@gmail.com"/>
    <add key="PFPassWord" value="xxxxxxxxxxxxx"/>
    <add key="smtpClient" value="smtp.gmail.com"/>
    <add key="SmtpServerPort" value="587"/>
  </appSettings>
</configuration>

and am calling this configuration in my ascx.cs file as bellow

using System.Configuration;
namespace SendingMessage.ControlTemplates.SendingMessage
{
    public partial class SendingMessage : UserControl
    {

        protected void btn1_Click(object sender, EventArgs e)
        {
            string userName = ConfigurationManager.AppSettings["PFUserName"];
            string password = ConfigurationManager.AppSettings["PFPassWord"];
            string smtpClient = ConfigurationManager.AppSettings["smtpClient"];
            string smtpPort = ConfigurationManager.AppSettings["SmtpServerPort"];
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient(smtpClient);
                mail.From = new MailAddress("xyz@gmail.com");
                mail.To.Add(lbl_mailId.Text.Trim());
                mail.Subject = "Test Mail";
                mail.Body = txt_mail.Text.Trim();
                SmtpServer.Port = int.Parse(smtpPort);
                SmtpServer.Credentials = new System.Net.NetworkCredential(userName, password);
                SmtpServer.EnableSsl = true;
                SmtpServer.Send(mail);
                txt_mail.Text = "";
            }
            catch (Exception ex)
            {

            }
        }
    }
}


but this above code is not working when am passing the configured data,so any one have any idea regarding my issue?
is there any other option which could be helpfull?

1

That is not how SharePoint works, web.config and app.config are features of web application and windows/console application respectively.

Below is what I can think of as alternative. Create your custom XML file or above file can also be used. You need to use module feature something to deploy your XML file to layouts folder which can go along with your ascx file.

From your ascx.cs file, use XML reader objects to read this XML file and Query required properties/nodes based on XML. Below can used to read XML document

XmlDocument doc = new XmlDocument();
doc.Load("c:\\temp.xml"); // path to your xml file from layouts...use http to read this...

Then read your nodes, you can also loop through all at once and store values in local variables...

XmlNode node = doc.DocumentElement.SelectSingleNode("/configuration/appSettings");

string text = node.InnerText;

Basically you need parse XML the way you are defining it..

https://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c

2
  • ,thank you so much for sujesting this approach it helped me to got what i want
    – Madhav
    Dec 29 '16 at 5:54
  • glad it helped!! Dec 29 '16 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.