Advertisement
Guest User

Untitled

a guest
May 31st, 2017
746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. using Microsoft.SharePoint.Client;
  2. using Microsoft.SharePoint.Client.Utilities;
  3. using SendGrid.Helpers.Mail;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Net.Mail;
  9. using System.Runtime.Remoting.Contexts;
  10. using System.Security;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13.  
  14. namespace Webtop.StarkGroup.MailReminder
  15. {
  16.     class Program
  17.     {
  18.         static void Main(string[] args)
  19.         {
  20.             string listName = "Kanban-Styling";
  21.             SecureString securePassword = new SecureString();
  22.  
  23.             string password = "JegByggerBroer94";
  24.  
  25.             foreach (char c in password.ToCharArray())
  26.             {
  27.                 securePassword.AppendChar(c);
  28.             }
  29.  
  30.             string siteURL = "https://theateam.sharepoint.com/sites/wizdom365";
  31.             string camlQuery = @"<View><Query>
  32.                                    <Where>
  33.                                        <Leq>
  34.                                            <FieldRef Name='DueDate' />
  35.                                                <Value IncludeTimeValue='False' Type='DateTime'><Today OffsetDays='3' /></Value>
  36.                                        </Leq>
  37.                                    </Where>
  38.                                </Query><RowLimit>100</RowLimit></View>";
  39.             ClientContext ctx = new ClientContext(siteURL);
  40.             ctx.Credentials = new SharePointOnlineCredentials("man@theateam.onmicrosoft.com", securePassword);
  41.             List list = ctx.Web.Lists.GetByTitle(listName);
  42.             CamlQuery caml = CamlQuery.CreateAllItemsQuery();
  43.             caml.ViewXml = camlQuery;
  44.             ListItemCollection items = list.GetItems(caml);
  45.             UserCollection users = ctx.Web.SiteUsers;
  46.             ctx.Load(items);
  47.             ctx.Load(users);
  48.             ctx.ExecuteQuery();
  49.            
  50.             foreach (var item in items)
  51.             {
  52.                 FieldUserValue[] userValue = (FieldUserValue[])item["AssignedTo"];
  53.                 if (userValue != null)
  54.                 {
  55.                     foreach (FieldUserValue user in userValue)
  56.                     {
  57.                         User spUser = users.GetById(user.LookupId);
  58.                         ctx.Load(spUser);
  59.                         ctx.ExecuteQuery();
  60.                         sendMail(spUser.Email, ctx);
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.  
  66.         public static void sendMail(string mailAddress, ClientContext ctx)
  67.         {
  68.             EmailProperties email = new EmailProperties();
  69.             email.To = new List<string>() { mailAddress };
  70.             email.Subject = string.Format("Subject");
  71.             email.Body = string.Format("<p>test mail</p>");
  72.  
  73.             Utility.SendEmail(ctx, email);
  74.             ctx.ExecuteQuery();
  75.            
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement