SharePoint Alerts not working correctly - sharepoint

I develop on SharePoint 2013 foundation. For some SharePoint list there is immediately created alert for special user. Alert created programmatically. User noticed about list items fields changing through email. So may question when list fields changed different times user receive email only for one change, why? For more information Immediately Alert timer job run after that fields changing. How to configure alert programmatically (C#) in order alert to work for two changes or there is another way?

Sample code to create alert.
SPList list=web.Lists.TryGetList("Documents");
SPUser user = web.EnsureUser(#"domainName\user");
SPAlert newAlert = user.Alerts.Add();
newAlert.Title = "My Custom Alert";
newAlert.AlertType=SPAlertType.List;
newAlert.List = list;
newAlert.DeliveryChannels = SPAlertDeliveryChannels.Email;
newAlert.EventType = SPEventType.Add;
newAlert.AlertFrequency = SPAlertFrequency.Immediate;
newAlert.Update();

Related

Sending out alerts when a certain option is chosen on a Sharepoint form

I have a SharePoint list, with two different views when creating a item in that list. Let us call these views, "regular user view" and "Approver user view" and the two views differ because one of the fields is hidden in the "regular user view" and is shown in the other view. Let us call this hidden form field "Approved or Decline". The particular group that the field is shown for, will come in after the regular user creates a new item and either changes the value of the "Approved or Decline" form field to either Approved or Declined based on if that person wants to accept or decline the request. How do I set up a alert, so if the "approver" declines a request, a notification is sent to the person that initially made that request?
That sounds like a SharePoint Workflow configured with SharePoint designer. Here is an overview of workflows: https://dev.office.com/sharepoint/docs/general-development/sharepoint-workflow-fundamentals
You would need to have the workflows automatically run when an item is created or modified and inside you would detect when the "Aprroved or Decline" field == "Declined" and in that case send an email to the original creator of the item.

Extract users from list, to a webpart SharePoint

I need to extract som information from a SharePoint list. Let us call this list for list_A. And in list_A I have column containing sharepoint users. That show their lync status (online, offline, busy etc), and when I hover over that user name I will get an "toolbox" that shows additional information. (See the picture)
What I now want is to extract that list of users into an SharePoint web part and have the ability to show the same information as in list_A. In other words I want to get the lync integration, with their status and the info box on hover. I figure I will manage to extract the information from the list without any problems using a method somewhat like the one below:
using (SPSite site = new SPSite(url))
{
using (SPWeb web = site.OpenWeb())
{
SPList lists = web.Lists["listName"];
foreach (SPListItem item in lists.Items)
{
string test = Convert.ToString(item["columnName"]);
TextBox1.Text += test;
}
}
}
But the question is if there is an asp or sharepoint controller that will allow me to show this kind field on the web page/in the web part?
Create a LiteralControl and use the following code to display the user presence in your webpart
("<span><img border=\"0\" height=\"12\" src=\"/_layouts/images/imnhdr.gif\" onload=\"IMNRC('" + userEmail+ "')\" ShowOfflinePawn=\"1\" alt=\"\" id=\"user_presence_icon\"/></span>");
There is a forum discussion on this topic and can be refered here -
Hope this helps

Find a specific field on a site and the set it to “Required” in a EventReciever

I have created a EventReciever that will be activated when a site gets created. This site have a announcement webpart. When they click on "add announcement", newform.aspx pops up as a dialog and there is a field there that is called Expired I need to make it required, so the field must be used else it prevent the user of adding a announcement.
How can I find that specific field and set it to Required in my EventReciever?
<!-- FieldName="Expires"
FieldInternalName="Expires"
FieldType="SPFieldDateTime"
-->
This is how I have progressed so far:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
if (properties.Feature.Parent is SPWeb)
{
var web = properties.Feature.Parent as SPWeb;
var expiredDate = web.Fields.GetFieldByInternalName("Expires");
expiredDate.Required = true;
expiredDate.Update();
}
}
Any kind of tips or help is appreciated!
Your WebPart will probably query a specific list? In that case it's also an option to set the Expires too required on that list.
Your current code will only update the Expires site column in the site, it will not push these changes to the lists using this column. If you were to create a new list after executing your code you would have a list where Expires is required.
However by reading your code my guess is that you actually want to update the current list and all new lists containing the column. In that case you should modify the expiredDate.Update() to expiredDate.Update(true). This boolean is an indication to SharePoint that you want the changes pushed down to existing lists. After executing this update you will see that your existing lists also require users to fill out an Expires date.

How to move/copy a discussion board item in Sharepoint?

How can I create a function that copies some item from one Sharepoint discussion board to another? Everything is quite simple except field name Posted By, MyEditor, and Editor.
If this is a one-time thing, you might look at using the Content Deployment API (there's a Content Deployment Wizard app that provides a UI for it).
Otherwise, you should be able to set the "posted by" by using something like this (assuming web is an open SPWeb and newItem is a discussion item):
SPUser user = web.Users["DOMAIN\\username"];
newItem[SPBuiltInFieldId.Author] = user.ID;
newItem[SPBuiltInFieldId.Editor] = user.ID;
newItem.SystemUpdate();

How to enable "Connect to Outlook" function in your custom list?

How can I enable the functionality to sync my custom list with Outlook 2007 todo or calendar list? I assume it's possible with custom lists too, but I cannot find any documentation on it.
To my knowledge this is not possible. If you need to sync a todo list, create a task list. Then you can delete all the columns that don't interest you, and add whatever columns you want to use.
The same goes for calendar, if you want a list syncronised with your outlook calendar, then create a calendar list and customize it to your needs.
To expand upon Filip's great answer . . .
According to this MSDN post, the custom list template has a different type designation (100) than the Task Template (106 in the article above, 107 in my research of SP2010). This is what the UI looks at to disable the Outlook Sync button. So, if your custom list is based on a custom content type you created then you can recreate your list to be Outlook compatible.
If your list already contains data, export it to Excel
Create a new list based on the Task template (instead of Custom)
Add your CT and delete the Task CT (if your content type is based on the Task CT i.e. has the columns) and any others your list doesn't need
Add any other columns your list needs but may not be part of your CT (or if you don't use a custom CT)
Use Datasheet view to copy and paste your data into the new list (observe column order)
Caveats: Not all data columns can be copied using this method (e.g. Attachments, system fields, etc.)
Hope this helps clarify
This is available / possible (well it takes some programming to achieve it) - see here.
For my solution, I aggregate data between sites and sync them with outlook. There are some tricky things like to map item's uniqueId to integer ID (probably a database table with mappings) to refer to specific item correctly, but it's not impossible.
But it's not something you can use right away - coding required. However if you really, really need it, then it's a some-way to go.
If your custom list is based off of a sharepoint list, then you can connect it to outlook by generating a link formatted based off the stssync protocol.
I created a custom list definition based off the events (Calendar) list.
It uses the base calendar list template plus some custom fields and forms.
I created a link formatted in accordance with the stssync protocol, and successfully connected it with outlook:
stssync://sts/?ver=1.1&type=calendar&cmd=add-folder&base-url=[site url]&list-url=[your list url]&guid=[GUID of list]&site-name=[SiteName]&list-name=[ListName]
Yeah not possible sadly. Also, if you create custom columns in a task list or calender, Outlook won't sync those columns.
I have been struggling with the same issue and found that it cannot be done with the built in functionality that SharePoint has, but there is a 3rd party program that allows this to be done. You can try with ProperSync. It will allow you to connect the custom fields of a custom list from SharePoint and view and edit them using Outlook.
This is the link to the main site: http://propersync.com/default.aspx
You can override List Ribbon Button, which holds sync logic, and create your custom action on it.
Here is good article about creating ribbons: http://blogs.msdn.com/b/sharepointdeveloperdocs/archive/2009/12/07/sharepointfoundationhowtoaddtabtoribbon.aspx
For List View OutlookSyncButton you should in CommandUIDefinition specify Location="Ribbon.List.Actions.ConnectToClient".
For Calendar View - you should specify Location="Ribbon.Calendar.Calendar.Actions.ConnectToClient".
Now, in CommandUIHandler/CommandAction you specifing JS code that connects to outlook (simply I grab it from SP :)):
alert("Let's go!");
var listid = '{ListId}';
var list;
ExecuteOrDelayUntilScriptLoaded(GetParamsAndSync, 'sp.js');
function GetParamsAndSync()
{
var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
list = web.get_lists().getById(listid);
clientContext.load(list);
clientContext.load(web);
rf = list.get_rootFolder()
clientContext.load(rf);
clientContext.executeQueryAsync(
function() {
viewUrl=rf.get_serverRelativeUrl();
ExportHailStormOverride('calendar','{SiteUrl}','{ListId}', web.get_title(),list.get_title(),viewUrl,viewUrl,'')
},
function() {alert('Error')});
}
//This function was taken from SharePoint sp.init.js
function ExportHailStormOverride(type,weburl,guid,webname,listname,viewurl,passport,listrooturl,folderurl,folderid)
{ULSA13:;
var strAppName=GetCookie('stsSyncAppName');
var strIconName=GetCookie('stsSyncIconPath');
var maxLinkLength=500;
var maxNameLength=20;
var link='stssync://sts/?ver=1.1'
+'&type='+escapeProperly(type)
+'&cmd=add-folder'
+'&base-url='+escapeForSync(weburl)
+'&list-url='+escapeForSync('/'
+ makeAbsUrl(viewurl).substr(weburl.length+1)+'/')
+'&guid='+escapeProperly(guid);
if (window.self.offlineBtnUser !=undefined)
link+='&user-id='+offlineBtnUser;
var names='&site-name='+escapeForSync(webname)
+'&list-name='+escapeForSync(listname);
var context='';
if (folderurl)
context+='&folder-url='
+ escapeForSync('/'+folderurl.substr(listrooturl.length+1));
if (folderid)
context+='&folder-id='+folderid;
if (link.length+names.length+context.length > maxLinkLength &&
(webname.length > maxNameLength || listname.length > maxNameLength))
{
if (webname.length > maxNameLength)
webname=webname.substring(0, maxNameLength-1)+'...';
if (listname.length > maxNameLength)
listname=listname.substring(0, maxNameLength-1)+'...';
names='&site-name='+escapeForSync(webname)
+'&list-name='+escapeForSync(listname);
}
link=link+names+context;
var L_StssyncTooLong_Text='The name of site or list is too long. Make it shorter.';
if (link.length > maxLinkLength)
alert(L_StssyncTooLong_Text);
else
{
try
{
window.location.href=link;
}
catch (e)
{
alert('Sync error.');
}
}
}

Resources