Thursday, December 08, 2005

Working with the Newsletter module for Sitecore V4


Here is the number of code snippets that might be useful: 1. Adding subscribers: Response.Write(" " + DateTime.Now.ToString()); MailingList list = new MailingList(); string mailinglistID = MasterFactory.GetItem("/sitecore/content/modules/mailing list/mailing lists/List1").ID; for (int i = 1; i <= 20000; i++) { string name = String.Concat("Name", i.ToString()); string company = ""; string country = Sitecore.State.Language; string email = String.Concat("Name", i.ToString(), "@company.com"); list.PutSubscriber(name, email, "", Sitecore.State.Language); list.Subscribe(name, email, mailinglistID); } Response.Write(" " + DateTime.Now.ToString()); 2. Edit subscriber’s mail: MailingList ml = new MailingList(); // getting the subscriber by e-mail XPathNavigator nav = ml.GetSubscriber("test@sitecore.net"); XmlNode node = MailingListHelper.ConvertToNode(nav); // retrieving subscriber's info string name = node.SelectSingleNode(@"/sitecore/subscriber/name").InnerText; string newEmail = "new@sitecore.net"; string company = node.SelectSingleNode(@"/sitecore/subscriber/company").InnerText; string country = node.SelectSingleNode(@"/sitecore/subscriber/country").InnerText; // adding a new subscriber with the new e-mail address ml.PutSubscriber(name, newEmail, company, country); // deleting the subscriber with old e-mail ml.DeleteSubscriber("test@sitecore.net"); 3. Reading mailing list: private void Page_Load(object sender, System.EventArgs e) { Sitecore.Modules.MailingList.MailingList mlist = new MailingList(); XPathNavigator xPathNavigator1 = mlist.GetSubscriber("at@sitecore.net"); if (xPathNavigator1 != null) { XPathNodeIterator xPathNodeIterator = xPathNavigator1.Select("/sitecore/subscriber"); if (xPathNodeIterator.MoveNext()) { XmlNode xmlNode1 = ((IHasXmlNode)xPathNodeIterator.Current).GetNode(); Response.Write(MailingListHelper.GetChildValue("name",xmlNode1)); Response.Write(" "); Response.Write(MailingListHelper.GetChildValue("email",xmlNode1)); Response.Write(" "); Response.Write(MailingListHelper.GetChildValue("company",xmlNode1)); Response.Write(" "); Response.Write(MailingListHelper.GetChildValue("country",xmlNode1)); Response.Write(" "); } } XmlNode node = MailingListHelper.ConvertToNode(mlist.GetMailingLists()); foreach(XmlNode list in node.SelectNodes("/sitecore/mailinglist")) { string nameList = XmlUtil.GetChildValue("name", list); string description = XmlUtil.GetChildValue("description", list); string ID = XmlUtil.GetAttribute("id", list); Response.Write("List name: " + nameList + " "); Response.Write("Description field: " + description + " "); Response.Write("List ID: " + ID + " "); IMasterItem itm = MasterFactory.GetItem(ID); if ( itm != null ) { string forTestOnly = itm.GetFieldValue("Test"); if ( forTestOnly.Equals("1") ) { Response.Write("For testing only is enabled" + " "); } else { Response.Write("For testing only is disabled" + " "); } } } XPathNavigator nav = mlist.GetMailingLists(); XPathNodeIterator node1 = nav.Select("/sitecore/mailinglist"); while (node1.MoveNext()) { string name = node1.Current.Evaluate("string(name)").ToString(); string id = node1.Current.GetAttribute("id", ""); XmlNode node2 = MailingListHelper.ConvertToNode( nav ); } 4. Read subscriber’s standard fields: Sitecore.Modules.MailingList.MailingList mlist = new MailingList(); XPathNavigator xPathNavigator1 = mlist.GetSubscriber("at@sitecore.net"); if (xPathNavigator1 != null) { XPathNodeIterator xPathNodeIterator = xPathNavigator1.Select("/sitecore/subscriber"); if (xPathNodeIterator.MoveNext()) { XmlNode xmlNode1 = ((IHasXmlNode)xPathNodeIterator.Current).GetNode(); Response.Write(MailingListHelper.GetChildValue("name",xmlNode1)); Response.Write(" "); Response.Write(MailingListHelper.GetChildValue("email",xmlNode1)); Response.Write(" "); Response.Write(MailingListHelper.GetChildValue("company",xmlNode1)); Response.Write(" "); Response.Write(MailingListHelper.GetChildValue("country",xmlNode1)); Response.Write(" "); } }