Two Web Parts connection doesn't work when set programmatically - sharepoint

I've a SharePoint 2010 page with a list. The list has several items and a field named "Department" and must filter items based on user's department value retrieved from user profile.
To do this I've created a feature which upon activation adds UserContextFilterWebPart to the page and makes connection between UserContextFilterWebPart and XsltListViewWebPart. After the feature is activated I can see in the page design mode that connection is established but the list gets empty. Then I open web part's menu, choose "Connections" then "Send Filter Values To" and click "List1". When dialog appears I do nothing but only click "Finish" button and it begins to work fine. Can anybody please explain me why the connection begins to work only if I do that manual extra action? What must be done to fix?
I tried different way when List.Views[0].Query property is set to appropriate CAML query and it also works fine. But I'm told that it's not a good approach because of performance and parallel tasks issues. Is it really bad course of action?
Below is the code for 2 different approaches.
Thanks in advance!
1-s variant with UserContextFilterWebPart:
SPSite Site = new SPSite(URL);
SPWeb Web = Site.OpenWeb();
SPLimitedWebPartManager WPM = Web.GetLimitedWebPartManager(URL, PersonalizationScope.Shared);
XsltListViewWebPart List = WPM.WebParts[0] as XsltListViewWebPart;
UserContextFilterWebPart UCFWP = new UserContextFilterWebPart();
UCFWP.Title = "Current User Filter";
UCFWP.AllowEdit = true;
UCFWP.FilterName = "Current User";
UCFWP.SendEmptyWhenNoValues = true;
UCFWP.AllowClose = true;
UCFWP.ExportMode = WebPartExportMode.All;
UCFWP.AllowConnect = true;
UCFWP.AllowHide = true;
UCFWP.ProfilePropertyName = "Department";
UCFWP.ValueKind = UserContextFilterValueKind.ProfileValue;
UCFWP.ZoneID = "Main";
WPM.AddWebPart(UCFWP, UCFWP.ZoneID, 1);
WPM.SaveChanges(UCFWP);
ConsumerConnectionPointCollection consumerConnections = WPM.GetConsumerConnectionPoints(List);
ConsumerConnectionPoint addConsumerConnPoint = consumerConnections["DFWP Filter Consumer ID"];
ProviderConnectionPointCollection providerConnections = WPM.GetProviderConnectionPoints(UCFWP);
ProviderConnectionPoint addProviderConnPoint = providerConnections["ITransformableFilterValues"];
TransformableFilterValuesToParametersTransformer trans = new TransformableFilterValuesToParametersTransformer();
trans.ConsumerFieldNames = new string[] { "Department" };
trans.ProviderFieldNames = new string[] { "Department" };
SPWebPartConnection newConnection = WPM.SPConnectWebParts(UCFWP, addProviderConnPoint, List, addConsumerConnPoint, trans);
WPM.SPWebPartConnections.Add(newConnection);
2-nd variant with CAML query (intended to be used not in a feature but in a web part):
SPSite Site = new SPSite(URL);
SPWeb Web = Site.OpenWeb();
SPLimitedWebPartManager WPM = Web.GetLimitedWebPartManager(URL, PersonalizationScope.Shared);
XsltListViewWebPart List = WPM.WebParts[0] as XsltListViewWebPart;
SPUser CurrentUser = Web.CurrentUser;
SPServiceContext context = SPServiceContext.GetContext(Site);
UserProfileManager upm = new UserProfileManager(context, false);
UserProfile up = upm.GetUserProfile(CurrentUser.RawSid);
String UserDepartment = up["Department"].Value.ToString();
SPView ListView = Web.Lists["List1"].Views[0];
ListView.Query = "<Where><Eq><FieldRef Name='Department' /><Value Type='Text'>" + UserDepartment + "</Value></Eq></Where>";
ListView.Update();

I had a similar problem of connecting two web parts. I found the answer here: http://kvdlinden.blogspot.dk/2011/02/programmatically-connect-two.html
Note that that post describes how to do it with two XsltListViewWebParts. In order to use it in your case I suggest that you:
Create the connection manually,
Use PowerShell to get a SPLimitedWebPartManager for the page,
Use the manager to iterate through the manager.SPWebPartConnections,
And find the ProviderConnectionPointID for your connection,
Use that ID in the code shown in the post.
Also remember to setup the transformer - you can find this also from the SPWebPartConnections.
Next time you activate your feature you should have a connection equal to the one you made by hand.

Related

SharePoint 2010: How do I connect FilterWebPart with ReportViewWebPart

I have the requirement to add performance point filter web part and report view web part to a page in SharePoint 2010 programmatically. I can add both web parts to the page however I have no idea on how to setup connection between them, i.e. for filter web part to be able to send its value to the report view web part.
Any help would be much appreciated.
Found the solution to this :)
What I did was when I create the connection using SPConnectWebParts, I use TransformableBIDataProviderTransformer object, e.g.
var list = new List<TransformProviderConsumerRecord>();
var transformer = new TransformableBIDataProviderTransformer();
var tpcRecord = new TransformProviderConsumerRecord();
tpcRecord = "SqlReportViewUniqueParameterIdSI1";
tpcRecord.ProviderParameterName = "FilterValues";
tpcRecord.DisplayColumnName = "DisplayValue";
tpcRecord.MappingId = (new Guid()).ToString();
tpcRecord.ProviderParameterDisplayName = "PerformancePoint Values";
tpcRecord.TypeFullName = "System.String";
tpcRecord.ValuesColumnName = "DisplayValue";
list.Add(transformProvConsRecord);
ProviderConsumerTransformations provConsTransf = new ProviderConsumerTransformations(list);
var tcr = new TransformerConfigurationRecord(provConsTransf, new TransformConditionalVisibilityRecord());
transformer.ConfigurationState = tcr;
wpm.SPConnectWebParts(providerWp, providerConnection, consumerWp, consumerConnection, transformer);
Where providerWP is Performance Point filter web part amd consumerWp is Performance Point Report

Getting term set of a user profile property in SharePoint 2010

I need to find out (programmatically) what term set is used by a certain user profile property, but I can't figure out how to do that. Any ideas are highly appreciated!
Finally, figured it out:
SPSite site = new SPSite(url);
SPServiceContext context = SPServiceContext.GetContext(site);
var userProfileConfigManager = new UserProfileConfigManager(context);
ProfilePropertyManager profilePropertyManager = userProfileConfigManager.ProfilePropertyManager;
CorePropertyManager corePropertyManager = profilePropertyManager.GetCoreProperties();
CoreProperty property = corePropertyManager.GetPropertyByName(propertyName);
TermSet ts = property.TermSet; // DONE!

Setting DisplayMemberPath of ComboBox in code

In my WPF program I have:
string queryString = "Select AccountID, ProjectName from Foo where IsEnabled = 1";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, sConn1);
DataSet dsAccounts = new DataSet();
adapter.Fill(dsAccounts, "Accounts");
cbAccount.ItemsSource = dsAccounts.Tables["Accounts"].AsEnumerable();
cbAccount.DisplayMemberPath = "ProjectName";
When my program runs and I dropdown the ComboBox all the rows are there but they display as blanks. When I click on a row, my SelectionChanged event handler properly identifies the selected row and picks up the proper values.
I believe my problem is with the DisplayMemberPath.
What am I doing wrong?
This is not an answer but rather a workaround. This works:
cbAccount.DataContext = dsAccounts.Tables["Accounts"];
//cbAccount.ItemsSource = dsAccounts.Tables["Accounts"].AsEnumerable();
cbAccount.DisplayMemberPath = "ProjectName";
By setting the DataContext reather than the ItemSource then the DisplayMemberPath is being set properly.
The question remains open, there must be a way to properly set the DisplayMemberPath when one has an ItemSource rather than a DataContext.
I believe the problem is that your table accounts is not serialized to objects.
If you use a list of accounts instead of your tables then it works perfect with the ItemsSource and DisplayMemeberPath.

sharepoint insert new item list

I am trying to insert new ListItems in a Sharepoint 2010 List already created. The code I'm using is:
ClientContext ctx = new ClientContext("http://bigboss/sites/presidencia");
Web thisWeb = ctx.Web;
ctx.Load(thisWeb);
List processosList = ctx.Web.Lists.GetByTitle("Processos");
ListItemCreationInformation lici = new ListItemCreationInformation();
ListItem liNovoProcesso = processosList.AddItem(lici);
liNovoProcesso["Title"] = processo.Identificador;
liNovoProcesso["IdentificadorProcesso"] = processo.Identificador;
liNovoProcesso["DescricaoProcesso"] = processo.Descricao;
liNovoProcesso["NotasObservacoesProcesso"] = processo.NotasObservacoes;
liNovoProcesso["SituacaoProcesso"] = processo.Situacao;
processosList.Update();
ctx.ExecuteQuery();
This code runs without erros or exceptions, but the list isn't showing the items. I've already tried to change Field names to trigger an error, just to be sure that the code its running, and it throws an error, like expected!
Any tips, please?
thanks,
José Cruz
Are you missing ?
liNovoProcesso.Update();
You should update the ListItem object instead of List.

Sharepoint Alerts on List Folders

I would like to programatically add alert to folders inside a sharepoint list. I have found how to set alerts to a list and this works perfect.
Could someone please help me on how to set alerts to a specific folder which is inside a list.
Below is the code i currently have which sets alerts only to the list.
using (SPSite site = new SPSite("http://site/"))
{
using (SPWeb web = site.OpenWeb())
{
SPUser user = web.SiteUsers["domain\\user"];
SPAlert newAlert = user.Alerts.Add();
newAlert.AlertType = SPAlertType.List;
newAlert.List = web.Lists["Documents"];
newAlert.EventType = SPEventType.All;
newAlert.AlertFrequency = SPAlertFrequency.Immediate;
//passing true to Update method will send alert confirmation mail
newAlert.Update(true);
}
}
Your help will be much appreciated
THIS QUESTION IS RESOLVED! PLEASE SEE MY POST BELOW WITH THE LINK - SEE - LINK
That's not possible out of the box, but after googling I found an interesting possibility though, check out Mike Walsh's answer on this post, it entails creating a view in the folder and then attaching the alert to that view.
You need to update the line with
newAlert.List = web.Lists["Documents"];
With
SPFolder fldr = web.GetFolder("/ListName/FolderName");
newAlert.Item=fldr.Item;
Also note that Folder is also another item.

Resources