Sharepoint calendar event attendees - c#

I want to programmatically add an event to a calendar in SharePoint 2010. I get the list of events and add element to this list.
SPList list = web.Lists.TryGetList(sCalendarName);
if (list != null)
{
SPListItem item = list.Items.Add();
item["Title"] = "New Event";
item["Description"] = "New Event created using SharePoint Object Model";
item["Location"] = "First Floor";
item["EventDate"] = DateTime.Now;
item["EndDate"] = DateTime.Now.AddDays(2);
item["Category"] = "Business";
item["fAllDayEvent"] = false;
item["Author"] = web.EnsureUser(#"domen\username");
item.Update();
}
But I can't find how to add a value to the "Particiants" ("Attendee") field.
If you look throw the item.Xml there is ows_ParticipantsPicker element, which contains users, that added by Sharepoint Calendar interface.
How can I add a participant (Attendee) to the event?

Have you tried using SPFieldUserValueCollection as the value of field?
SPFieldUserValueCollection values = new SPFieldUserValueCollection();
SPUser user = web.EnsureUser(#"domen\username");
values.Add(new SPFieldUserValue(web, user.ID, user.Name));
item["Participants"] = values;
Also, don't use SPList.Items.Add(), it gets all items before adding a new one. Use SPList.AddItem().

Related

EWS search filter to find sender domain C#

I've create a small application that gets all my emails. I add each email to a list. However before I add them to a list I filter them. All of my filters are working apart from one. My search filter for filtering my email by sender is not working as I'm only trying to filter the domain and not the whole email address. For example xxx#xxx.com will filter however I want to filter out everything with xxx.com domain and for some reason it doesn't filter it. I tried using substring and this doesn't work either.
My code is as fallows
private static SearchFilter.SearchFilterCollection sFilter = new SearchFilter.SearchFilterCollection();
private static FindItemsResults<Item> findResults;
sFilter.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(EmailMessageSchema.Sender, "#xxx.com",ContainmentMode.Substring,ComparisonMode.IgnoreCase)));
sFilter.Add(new SearchFilter.Not(new SearchFilter.Exists(EmailMessageSchema.InReplyTo)));
DateTime startTime = GetDateValueforFilter();
startTimefilter = new SearchFilter.IsGreaterThanOrEqualTo(EmailMessageSchema.DateTimeReceived, startTime);
sFilter.Add(startTimefilter);
sFilter.Add(startTimefilter);
findResults = service.FindItems(
WellKnownFolderName.Inbox
,sFilter
,new ItemView(25));
foreach (EmailMessage item in findResults.Items)
{
//if (item.IsRead == false)
//{
// if (item.InReplyTo == null)
// {
bool replyToAll = true;
string myReply = "This is the message body of the email reply.";
item.Reply(myReply, replyToAll);
item.IsRead = true;
item.Send();
//}
//}
}
I would suggest you try using the Extended Property PidTagSenderSmtpAddress https://msdn.microsoft.com/en-us/library/office/jj713594.aspx and try something like
ExtendedPropertyDefinition PidTagSenderSmtpAddress = new ExtendedPropertyDefinition(0x5D01,MapiPropertyType.String);
SearchFilter sf = new SearchFilter.ContainsSubstring(PidTagSenderSmtpAddress, "#yahoo.com");

Updating a field item's value in a Sharepoint form via C# - value not displaying?

I'm having trouble with a field on my form displaying. It's a DateTime data type and is being used to track how many days an item has been at that stage of production. I tried converting the item.["DaysInInitation"] to an Int, to see if that would work but nothing displays still. Could anyone point me in the right direction? Here is my code:
ClientContext ctx = new ClientContext(URL);
ctx.Credentials = new SharePointOnlineCredentials(Username, password);
List list = ctx.Web.Lists.GetByTitle(ListName);
ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery());
ctx.Load(items); // loading all the fields
ctx.ExecuteQuery();
foreach (var item in items)
{
// important thing is, that here you must have the right type
// i.e. item["Modified"] is DateTime
//item["fieldName"] = newValue;
if (item.Id == fo.docid)
{
item.["DaysInInitation"] = fo.ini_di;
item.Update();
}
// do whatever changes you want
item.Update(); // important, rembeber changes
}
ctx.ExecuteQuery(); // important, commit changes to the server

Best way to add multiple rooms in Outlook appointment programatically

I'm implementing an Outlook plugin and I need to create appointments by code, the appointments I want to model occur in rooms, rooms live in Outlook Exchange. How can I add various rooms to the appointment created by code, consider the following code to create an appointment:
Outlook.AppointmentItem newAppointment =
(Outlook.AppointmentItem)
this.Application.CreateItem(Outlook.OlItemType.olAppointmentItem);
newAppointment.Start = DateTime.Now.AddHours(2);
newAppointment.End = DateTime.Now.AddHours(3);
newAppointment.Location = "SOME_LOCATION";
newAppointment.Body =
"We will discuss progress on the group project.";
newAppointment.AllDayEvent = false;
newAppointment.Subject = "Group Project";
newAppointment.Recipients.Add("Roger Harui");
Outlook.Recipients sentTo = newAppointment.Recipients;
Outlook.Recipient sentInvite = null;
sentInvite = sentTo.Add("Holly Holt");
sentInvite.Type = (int)Outlook.OlMeetingRecipientType
.olRequired;
sentInvite = sentTo.Add("David Junca ");
sentInvite.Type = (int)Outlook.OlMeetingRecipientType
.olOptional;
sentTo.ResolveAll();
newAppointment.Save();
newAppointment.Recipients.ResolveAll();
newAppointment.Display(true);
You add a room the same way you add an attendee - pass the name of the room to Recipients.Add and set the Recipient.Type property to olResource (3).

How to delete all event entries from a specific calendar from Google calendar API .NET

I am trying editing a tool to allow a user to select from a list of their calendars and then clear all event entries / add new ones based on Microsoft project tasks.
Heres the original tool: http://daball.github.com/Microsoft-Project-to-Google-Calendar/
I am completely unexperienced with Google APIs / the calendar API, and am having some trouble. The program I'm editing keeps track of which CalendarEntry the user has selected from a list of their calendars. What I am currently trying to do is create a EventFeed which gives me the EventEntries of that selected calendar, so I can then delete all of them. The purpose of this is to allow someone to use this tool to also update the calendar from the project file whenever changes are made. Here's my function attempting the delete.
private void clearPreviousCalendarEntries(CalendarEntry calendarEntry)
{
EventQuery query = new EventQuery();
query.Uri = new Uri(calendarEntry.Links[0].AbsoluteUri);
EventFeed feed = (EventFeed)calendarService.Query(query);
AtomFeed batchFeed = new AtomFeed(feed);
foreach (EventEntry entry in feed.Entries)
{
entry.Id = new AtomId(entry.EditUri.ToString());
entry.BatchData = new GDataBatchEntryData(GDataBatchOperationType.delete);
batchFeed.Entries.Add(entry);
}
EventFeed batchResultFeed = (EventFeed)calendarService.Batch(batchFeed, new Uri(feed.Batch));
foreach (EventEntry entry in batchResultFeed.Entries)
{
if (entry.BatchData.Status.Code != 200 && entry.BatchData.Status.Code != 201)
this.listBoxResults.SelectedIndex = this.listBoxResults.Items.Add("Problem deleteing " + entry.Title.Text + " error code: " + entry.BatchData.Status.Code);
else
this.listBoxResults.SelectedIndex = this.listBoxResults.Items.Add("Deleted " + entry.Title.Text);
}
}
My feed doesn't return the results I was hoping for, but to be honest I'm not sure how to request the events correctly.
query.Uri = new Uri(calendarEntry.Links[0].AbsoluteUri); is something I grabbed from the portion of the program which is adding event to a specific calendar
AtomEntry insertedEntry = calendarService.Insert(new Uri(calendarEntry.Links[0].AbsoluteUri), eventEntry);
These posts are definitely related to what I'm looking for but I haven't arrived at a solution
google-calendar-get-events-from-a-specific-calendar
how can i retrieve a event exclusive from a calendar that i created (not default one)?
Try something like this:
CalendarService myService = new CalendarService("your calendar name");
myService.setUserCredentials(username, password);
CalendarEntry calendar;
try
{
calendar = (CalendarEntry)myService.Get(http://www.google.com/calendar/feeds/default/owncalendars/full/45kk8jl9nodfri1qgepsb65fnc%40group.calendar.google.com);
foreach (AtomEntry item in calendar.Feed.Entries)
{
item.Delete();
}
}
catch (GDataRequestException)
{
}
You can find "Calendar ID" (something like this: 45kk8jl9nodfri1qgepsb65fnc%40group.calendar.google.com) from Calendar Details page inside Google Calendar.
this is a related post:
google calendar api asp.net c# delete event
this is a useful doc:
http://code.google.com/intl/it-IT/apis/calendar/data/2.0/developers_guide_dotnet.html
A way I eventually arrived at was gathering the calendarID from the URI of the calendar, and then creating a new EventQuery using that id. Here's the new version of the code above
private void clearPreviousCalendarEntries(CalendarEntry calendarEntry)
{
this.listBoxResults.SelectedIndex = this.listBoxResults.Items.Add("Clearing previous calender entries");
String calendarURI = calendarEntry.Id.Uri.ToString();
//The last part of the calendarURI contains the calendarID we're looking for
String calendarID = calendarURI.Substring(calendarURI.LastIndexOf("/")+1);
EventQuery query = new EventQuery();
query.Uri = new Uri("http://www.google.com/calendar/feeds/" + calendarID + "/private/full");
EventFeed eventEntries = calendarService.Query(query);
AtomFeed batchFeed = new AtomFeed(eventEntries);
foreach (AtomEntry entry in eventEntries.Entries)
{
entry.Id = new AtomId(entry.EditUri.ToString());
entry.BatchData = new GDataBatchEntryData(GDataBatchOperationType.delete);
batchFeed.Entries.Add(entry);
}
EventFeed batchResultFeed = (EventFeed)calendarService.Batch(batchFeed, new Uri(eventEntries.Batch));
//check the return values of the batch operations to make sure they all worked.
//the insert operation should return a 201 and the rest should return 200
bool success = true;
foreach (EventEntry entry in batchResultFeed.Entries)
{
if (entry.BatchData.Status.Code != 200 && entry.BatchData.Status.Code != 201)
{
success = false;
listBoxResults.SelectedIndex = listBoxResults.Items.Add("The batch operation for " +
entry.Title.Text + " failed.");
}
}
if (success)
{
listBoxResults.SelectedIndex = listBoxResults.Items.Add("Calendar event clearing successful!");
}
}
I'm not particular happy with this, it seems odd to use string manipulation to gather the info and chop together my own query. But it works and I have been struggling to find a way to get this done.

Retrieve all items from a SharePoint Field Choice Column

I am playing around with a SharePoint server and I am trying to programmatically add a service request to microsoft's call center application template. So far, I have had pretty good success. I can add a call for a specified customer and assign a specific support tech:
private enum FieldNames
{
[EnumExtension.Value("Service Request")]
ServiceRequest,
[EnumExtension.Value("Customer")]
Customer,
[EnumExtension.Value("Service Representative")]
ServiceRepresentative,
[EnumExtension.Value("Assigned To")]
AssignedTo,
[EnumExtension.Value("Software")]
Software,
[EnumExtension.Value("Category")]
Category
}
private void CreateServiceCall(string serviceCallTitle, string customerName, string serviceRep)
{
SPSite allSites = new SPSite(siteURL);
SPWeb site = allSites.AllWebs[siteName];
SPListItemCollection requestsList = site.Lists[serviceRequests].Items;
SPListItem item = requestsList.Add();
SPFieldLookup customerLookup = item.Fields[FieldNames.Customer.Value()] as SPFieldLookup;
item[FieldNames.ServiceRequest.Value()] = serviceCallTitle;
if (customerLookup != null)
{
using (SPWeb lookupWeb = allSites.OpenWeb(customerLookup.LookupWebId))
{
SPList lookupList = lookupWeb.Lists.GetList(new Guid(customerLookup.LookupList), false);
foreach (SPListItem listItem in lookupList.Items)
{
if (listItem[customerLookup.LookupField].ToString() != customerName) continue;
item[FieldNames.Customer.Value()] = new SPFieldLookupValue(listItem.ID, customerName);
break;
}
}
}
SPUserCollection userCollection = site.SiteUsers;
if (userCollection != null)
{
foreach (SPUser user in userCollection)
{
if (user.Name != serviceRep) continue;
item[FieldNames.AssignedTo.Value()] = user;
break;
}
}
item.Update();
site.Close();
allSites.Close();
}
I added two custom columns (category, software) to the default list:
I populated both of these columns inside of SharePoint, now I want to retrieve that data so I can use it in the code snippet I posted to assign the proper category/software etc to the call. I have not been able to get the list in the code, I have tried using a item["Software"], site.Lists["Software"] and a couple of others, but so far all I have come up is null.
Can anyone point me in the right direction for this? Thanks!
SPFieldMultiChoice and related fields have a Choices property:
SPFieldMultiChoice software = item.Fields[FieldNames.Software.Value()] as SPFieldMultiChoice;
StringCollection softwareChoices = software.Choices;
If you need to set a value on the field, use the SPFieldMultiChoiceValue type:
SPFieldMultiChoiceValue values = new SPFieldMultiChoiceValue();
values.Add("Choice 1");
values.Add("Choice 2");
item[FieldNames.Software.Value()] = values;

Categories

Resources