Question

I need to send an Email with a list items name of the rows my caml query picks out. I have this code:

                SPQuery filter = new SPQuery();
                filter.Query = string.Format("<Where><Leq><FieldRef Name=\"Revisionsdatum\" /><Value Type=\"DateTime\">{0}</Value></Leq></Where>", DateTime.Today.AddDays(14).ToString("yyyy-MM-ddThh:mm:ssZ"));
                SPListItemCollection items = yourList.GetItems(filter);
                foreach (var i in items)
                {
                    string from = string.Empty;
                    string smtpAddress = string.Empty;
                    string to = "Someone@someCompany.com";                    
                    string subject = "Dudate is coming";
                    string body = "<h1>Hello!</h1><p>In to weeks an importent dudates comes({0}) with the name {2}.";//Here I would like to ad the dudate and the ListItems Name but how?

                    // get a reference to the current site collection's content database 
                    SPWebApplication webApplication = this.Parent as SPWebApplication;
                    SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];

                    // get a reference to the "Tasks" list in the RootWeb of the first site collection in the content database 
                    SPWeb rootWeb = contentDb.Sites[0].RootWeb;

                    SPList listjob = rootWeb.Lists.TryGetList("Tasks");

                    // Get sender address from web application settings 
                    from = rootWeb.Site.WebApplication.OutboundMailSenderAddress;

                    // Get SMTP address from web application settings 
                    smtpAddress = rootWeb.Site.WebApplication.OutboundMailServiceInstance.Server.Address;

                    // Send an email if the news is approved 
                    bool emailSent = SendMail(smtpAddress, subject, body, true, from, to, null, null);


                }                

I would bee greatfull for your answer!

Was it helpful?

Solution

You can get it as follows:

foreach (SPListItem item in items) \\ items is your SPListItemCollection 
{
    var fieldValue = item["Field Name"].ToString();
}

OTHER TIPS

To convert DateTime object to CAML query use SPUtility.CreateISO8601DateTimeFromSystemDateTime() method.

Fields you need are referenced by i["Title"] and i["DueDate"].

Use StringBuilder object in foreach loop to construct the body of your mail and send the email after the loop. Your code will send one mail for each task.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top