C# - Visual Studio - 'object' does not contain a definition for 'ExecuteQuery' - c#

I am trying to add code to an existing project that will check for existence of a device in SCCM and delete it if it does exist. I seem to be missing something, in that particular block of code. I get an error - 'object' does not contain a definition for 'ExecuteQuery' and no extension method 'ExecuteQuery' accepting a first argument of type 'object' could be found.
Here is the C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.NetworkInformation;
using SupportToolkit.Models;
using SupportToolkit.WindowsAutomationServices;
using NLog;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.Management;
namespace SupportToolkit.Controllers
{
public class TPOSRecordDeletionController : Controller
{
private static Logger recordDeletionLogger = LogManager.GetCurrentClassLogger();
[Authorize(Roles = #"nor\NST_RIT_Users,nor\NST_STM_Users,nor\NST_Admin_Users,nor\NST_CORP_Users")]
// GET: TPOSRecordDeletion
public ActionResult Index(TPOSRecordDeletionModel model)
{
if (model.ComputerName != null)
{
}
if (ModelState.IsValid)
{
if (!(string.IsNullOrEmpty(model.btnDeleteRecord)))
{
InvokeRecordDeletion(model);
}
}
return View(model);
}
[Authorize(Roles = #"nor\NST_RIT_Users,nor\NST_STM_Users,nor\NST_Admin_Users,nor\NST_CORP_Users")]
public string InvokeRecordDeletion(TPOSRecordDeletionModel model)
{
model.Status = "Running Service";
var windowsAutomationService = new WindowsAutomationServicesClient();
string shortServiceOutput;
var serviceAction = "Remove-TPOSRecords";
var SCCMServer = "server.nor.net";
var siteCode = "PO60";
string[] recordDeletionArguments = new string[1];
recordDeletionArguments[0] = model.ComputerName;
model.Status = "Processing" + model.ComputerName;
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
options.DontFragment = true;
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
PingReply reply = pingSender.Send(model.ComputerName, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
model.Status = model.ComputerName + "is currently online and will not be removed!";
}
else
{
// set up domain context
using (var ctx = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain))
{
// find a computer
ComputerPrincipal computer = ComputerPrincipal.FindByIdentity(ctx, model.ComputerName);
if (computer == null)
{
model.Status = model.ComputerName + "does not exist in Active Directory.";
}
else
{
computer.Delete();
model.Status = model.ComputerName + "successfully removed from Active Directory!";
}
//insert code here for checking for existence of computer in SCCM and removing from SCCM if exists
SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();
WqlConnectionManager connection = new WqlConnectionManager(namedValues);
connection.Connect("s0319p60.nordstrom.net");
foreach (IResultObject computerobject in connection.QueryProcessor.ExecuteQuery("Select ResourceID From SMS_R_System Where Name ='" + model.ComputerName + "'"))
{
if (computerobject == null)
{
model.Status = model.ComputerName + "does not exist in SCCM.";
}
else
{
computerobject.Delete();
model.Status = model.ComputerName + "successfully removed from SCCM!";
}
}
}
var userName = User.Identity.Name;
var serviceOutput = windowsAutomationService.RunAutomationService(serviceAction, userName, recordDeletionArguments);
recordDeletionLogger.Info(userName + " is attempting to remove the record " + model.ComputerName);
if (serviceOutput.Length >= 7)
{
shortServiceOutput = serviceOutput.Substring(0, 7);
shortServiceOutput = shortServiceOutput.ToLower();
}
else
{
shortServiceOutput = serviceOutput;
shortServiceOutput = shortServiceOutput.ToLower();
}
if (shortServiceOutput == "success")
{
model.Status = "Successfully removed " + model.ComputerName + " from SCCM and Active Directory";
recordDeletionLogger.Info(userName + " successfully removed " + model.ComputerName + " from SCCM and Active Directory");
return "Success";
}
else
{
model.Status = "Failure removing " + model.ComputerName + " from SCCM and Active Directory. Unknown Error";
recordDeletionLogger.Info(userName + " failed to remove " + model.ComputerName + " from SCCM and Active Directory");
return "Failure";
}
}
}
}
internal interface IResultObject
{
//void Delete();
void Delete();
}
internal class WqlConnectionManager
{
private SmsNamedValuesDictionary namedValues;
public WqlConnectionManager(SmsNamedValuesDictionary namedValues)
{
this.namedValues = namedValues;
}
public object QueryProcessor { get; internal set; }
internal void Connect(string v)
{
throw new NotImplementedException();
}
public object ExecuteQuery { get; internal set; }
}
internal class SmsNamedValuesDictionary
{
public SmsNamedValuesDictionary()
{
}
}

Well - after a few days of searching, I FINALLY figured out the issue.
The entire block of code at the end, was not necessary. The issue was that there were missing assembly references and using statements in the code. Specifically:
using Microsoft.ConfigurationManagement.ManagementProvider;
using Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine;
The corresponding DLLs also needed to be added to the project as references.
I hope this helps someone who runs into similar issues and is not versed in C# coding. I myself am only versed in PowerShell, so figuring this out took a lot of work for me. Thanks.

You create a custom "connection" object:
WqlConnectionManager connection = new WqlConnectionManager(namedValues);
Then call a method on one of its properties:
connection.QueryProcessor.ExecuteQuery("...")
But what is that QueryProcessor property?...
public object QueryProcessor { get; internal set; }
It's an object. As the error states, object doesn't have a method called ExecuteQuery. (It doesn't have very many methods or properties at all, really.)
I can't really tell from this code (maybe I'm missing something?) what specific type you're expecting QueryProcessor to be, but it should definitely be something more specific than object. Something analogous to a SQLCommand object, perhaps? Essentially, whatever type would have that ExecuteQuery method.
If there's a compelling reason in the existing codebase for this to be of type object, you'd need to determine what that reason is. There seems to be a lot of use of object here, which smells of some bad design choices from before you got there.

Related

C#: Multithreaded (task) PowerShell cmdlet

Per the subject, I'm trying to put together a multithreaded PowerShell cmdlet and am struggling. I can get the code to run successfully as a console app, and I can get the single threaded cmdlet to work, but haven't been able to merge the two as of yet.
Also, please understand that I am new to C# and have very little clue about what I'm doing, so apologies in advance for the atrocity that is my code. Tried to simplify as much as I could before posting.
Here's what I've got so far:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
using System.DirectoryServices;
using System.Diagnostics;
using System.Threading;
namespace Sample
{
[Cmdlet(VerbsCommon.Get,"LDAPObject")]
[OutputType(typeof(List<SearchResult>))]
public class GetLDAPObjectCmdlet: Cmdlet
{
[Parameter(ValueFromPipeline = true)]
public string searchRoot { get; set; }
[Parameter]
public TimeSpan maxWaitTime { get; set; }
private List<Task<searchResults>> taskList = new List<Task<searchResults>>();
protected override void BeginProcessing()
{
base.BeginProcessing();
}
protected override void ProcessRecord()
{
base.ProcessRecord();
var ts = new CancellationTokenSource();
CancellationToken ct = ts.Token;
Task<searchResults> searchTask = Task<searchResults>.Factory.StartNew(() => Search(ct, searchRoot));
taskList.Add(searchTask);
Stopwatch sw = Stopwatch.StartNew();
bool tasksCompleted = new bool();
try
{
tasksCompleted = Task.WaitAll(taskList.ToArray(), maxWaitTime);
}
catch (OperationCanceledException ex)
{
WriteWarning("Task cancelled.");
}
int resultCount = 0;
foreach (Task<searchResults> taskItem in taskList)
{
if ((taskItem.Status == TaskStatus.RanToCompletion) && (taskItem.Result.src != null))
{
resultCount += taskItem.Result.src.Count;
}
foreach (string message in taskItem.Result.warningMessages)
{
WriteWarning(message);
}
}
sw.Stop();
if (tasksCompleted)
{
WriteVerbose("The search returned `'" + resultCount.ToString() + "' results in '" + (sw.Elapsed.TotalSeconds).ToString() + "' seconds.");
}
else
{
WriteVerbose("The search did not complete before the deadline.");
}
}
protected override void EndProcessing()
{
base.EndProcessing();
for (int i = 0; i < taskList.Count; i++)
{
taskList[i].Dispose();
}
}
protected override void StopProcessing()
{
base.StopProcessing();
}
private static searchResults Search(CancellationToken ct, string searchRootDn)
{
if (ct.IsCancellationRequested)
{
ct.ThrowIfCancellationRequested();
}
searchResults results = new searchResults();
results.warningMessages = new List<string>();
results.verboseMessages = new List<string>();
results.warningMessages.Add("Search root: '" + searchRootDn + "'.");
DirectoryEntry searchRoot = new DirectoryEntry("LDAP://" + searchRootDn);
try
{
Guid guid = searchRoot.Guid;
}
catch (Exception ex)
{
throw new Exception("Invalid search root");
}
string searchFilter = "some LDAP filter here";
results.warningMessages.Add("Search filter: '" + searchFilter + "'.");
DirectorySearcher directorySearcher = new DirectorySearcher();
directorySearcher.SearchRoot = searchRoot;
directorySearcher.SearchScope = SearchScope.Subtree;
directorySearcher.PageSize = 1000;
directorySearcher.Filter = searchFilter;
results.src = directorySearcher.FindAll();
directorySearcher.Dispose();
return results;
}
}
class searchResults
{
public SearchResultCollection src { get; set; }
public List<string> warningMessages { get; set; }
public List<string> verboseMessages { get; set; }
}
}
This gives me:
System.InvalidCastException
HResult=0x80004002
Message=Unable to cast COM object of type 'System.__ComObject' to interface type 'IDirectorySearch'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{109BA8EC-92F0-11D0-A790-00C04FD8D5A8}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Source=System.DirectoryServices
StackTrace:
at System.DirectoryServices.SearchResultCollection.get_SearchObject()
at System.DirectoryServices.SearchResultCollection.ResultsEnumerator.MoveNext()
at System.DirectoryServices.SearchResultCollection.get_InnerList()
at System.DirectoryServices.SearchResultCollection.get_Count()
at MultiThreadedSearch.GetLDAPObjectCmdlet.ProcessRecord() in C:\Users\obfuscated\GETLDAPObjectCmdlet.cs:line 73
at System.Management.Automation.CommandProcessor.ProcessRecord()
on the resultCount += taskItem.Result.src.Count; line.
Note that I didn't actually need the searchResult class; I just introduced it for want of a means by which to allow the Search function to be able to contribute content to WriteWarning, WriteVerbose, etc.
Thoughts are most appreciated!
Looks like I solved my own problem. Unexpectedly, the problem appears to stem from how the DirectorySearcher interface is implemented. Specifically, the results from the LDAP search are not cached until the first call to enumerate the SearchResultsCollection - specifically, when I attempted to access the Count property. This is nicely described here:
We tend to use the foreach statement to enumerate the results. Unlike some other collection classes in the .NET Framework, SearchResultCollection internally implements its own private IEnumerator object that pulls the results from the directory. Any access to the SearchResultCollection properties, such as inspecting the Count property, will cause the entire result set to be retrieved from the directory and then enumerated. This can have a large impact, especially when returning many results, for a couple of reasons. First, using a foreach loop would allow us to break off the search if necessary, without enumerating all the results completely. This is more efficient, as we break off the search early and do not force the server to return all of the results first. Second, we can process each result as it comes from the server using a foreach loop without waiting for the entire search to complete. Using a for loop in conjunction with the Count property prevents both of these scenarios. As such, we generally recommend using the foreach loop when enumerating our results.

Calling a static method in C#

I implemented a plugin (using pGina software) to allow the user to authenticate the username/password in their computer by scanning a NFC tag.
I used a program I found called CSharp PC/SC Wrapper for .NET to read the tag ID. Every time a tag is scanned the program writes the ID to a text file and checks that the ID is the the same as the one set on the string.
if (userInfo.Username.Contains("hello") && userInfo.Password.Contains("pGina")
&& text.Equals("UID = 0x04 82 EC BA 7A 48 80"))
The plugin is set to find the .exe file that reads the ID (PC/SC Wrapper). Everything works fine. However, I don't one the reader program to be in a different file. I want everything to be in the plugin file.
I created a method and copied the code from the wrapper that performs the reading of the tag ID (runme()), but I'm not sure how to replace the line that calls the .exe file with the method I created
ProcessStartInfo ps = new ProcessStartInfo(#"C:\Users\Student\Desktop\CSharpPCSC\CSharpPCSC\ExamplePCSCReader\bin\Release\ExamplePCSCReader.exe");
Any suggestions? I'm new to C#
Below is my code for the plugin with the method containing the code that reads the ID
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using pGina.Shared.Types;
using log4net;
using System.IO;
using System.Diagnostics;
using GS.PCSC;
using GS.Apdu;
using GS.SCard;
using GS.Util.Hex;
using System.Threading;
namespace HelloPlugin
{
public class PluginImpl : pGina.Shared.Interfaces.IPluginAuthentication
{
private ILog m_logger;
private static readonly Guid m_uuid = new Guid("CED8D126-9121-4CD2-86DE-3D84E4A2625E");
public PluginImpl()
{
m_logger = LogManager.GetLogger("pGina.Plugin.HelloPlugin");
}
public string Name
{
get { return "Hello"; }
}
public string Description
{
get { return "Authenticates users with 'hello' in the username and 'pGina' in the password"; }
}
public Guid Uuid
{
get { return m_uuid; }
}
public string Version
{
get
{
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
}
public void Starting()
{
}
public void Stopping() { }
public BooleanResult AuthenticateUser(SessionProperties properties)
{
UserInformation userInfo = properties.GetTrackedSingle<UserInformation>();
ProcessStartInfo ps = new ProcessStartInfo(#"C:\Users\Student\Desktop\CSharpPCSC\CSharpPCSC\ExamplePCSCReader\bin\Release\ExamplePCSCReader.exe");
Process.Start(ps);
Thread.Sleep(2000);
string text = File.ReadAllText(#"C:\Users\Student\Desktop\text.txt", Encoding.UTF8);
text = text.Trim();
if (userInfo.Username.Contains("hello") && userInfo.Password.Contains("pGina") && text.Equals("UID = 0x04 82 EC BA 7A 48 80"))
{
// Successful authentication
m_logger.InfoFormat("Successfully authenticated {0}", userInfo.Username);
return new BooleanResult() { Success = true };
}
// Authentication failure
m_logger.ErrorFormat("Authentication failed for {0}", userInfo.Username);
return new BooleanResult() { Success = false, Message = "Incorrect username or password." };
}
static void runme()
{
ConsoleTraceListener consoleTraceListener = new ConsoleTraceListener();
Trace.Listeners.Add(consoleTraceListener);
PCSCReader reader = new PCSCReader();
string cardid = "";
try
{
reader.Connect();
reader.ActivateCard();
RespApdu respApdu = reader.Exchange("FF CA 00 00 00"); // Get NFC Card UID ...
if (respApdu.SW1SW2 == 0x9000)
{
Console.WriteLine("UID = 0x" + HexFormatting.ToHexString(respApdu.Data, true));
cardid = "UID = 0x" + HexFormatting.ToHexString(respApdu.Data, true);
cardid = cardid.Trim();
}
}
catch (WinSCardException ex)
{
Console.WriteLine(ex.WinSCardFunctionName + " Error 0x" +
ex.Status.ToString("X08") + ": " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
string path = #"C:\Users\Student\Desktop\text.txt";
string text2write = cardid;
System.IO.StreamWriter writer = new System.IO.StreamWriter(path);
writer.Write(text2write);
writer.Close();
reader.Disconnect();
Environment.Exit(0);
Console.WriteLine("Please press any key...");
Console.ReadLine();
}
}
}
}
You've created a class called PluginImpl and in that class declared the method runme. To call that method from anywhere, you need to write PluginImpl.runme().
Since you've put your class in the namespace HelloPlugin - if the calling *.cs file is in a different namespace, you'll need a using HelloPlugin directive at the top.
That's all!
It's possible I have misunderstood your question, if so please re-word your question and send me a comment.
If you want to replace the line
ProcessStartInfo ps = new ProcessStartInfo(
#"C:\Users\Student\Desktop\CSharpPCSC\CSharpPCSC\"
+"ExamplePCSCReader\bin\Release\ExamplePCSCReader.exe");
with a method call instead, you want something like this
ProcessStartInfo ps = runme();
Since you are calling your static method from within the class, you don't need a PluginImpl. prefix.
Okay, so now it will complain that runme doesn't return ProcessStartInfo. You're going to need to change runme so that it does. Any subclass of ProcessStartInfo will do.
static ProcessStartInfo runme()
{
// ... Some code
ProcessStartInfo toReturn = new ProcessStartInfo( //...
);
// ... More code
return toReturn;
}

Revit API - Wrong full class name

I'm very new to C# and coding. If possible I'm after some assistance figuring out how to fix this piece of code up to work.
They work individually. I can create a new button on the ribbon and execute the standard hello world. i also have a macro which works where I can remove all my sheets and views successfully but trying to combine the two is causing big difficulties! The code builds OK but I'm getting this error inside revit:
'Failed to initialize the add-in "Delete Views" because the class
"DeleteViews" cannot be found in the add-in assembly.The FullClassName
provides the enrty point for Revit to call add-in application. For
Revit to run the add-in, you must ensure this class implements the
"Autodesk.Revit.UI.ExternalCommand" interface.'
I'm pretty sure my problem is referencing in this 2nd bit of code. i know I'm not calling it up correctly but haven't been able to find any solutions.
Apologies if this is a dumb question but would really appreciate any help to help me learn!
Thanks for any help you can give me
The code:
namespace BGPanel
{
public class CsBGPanel : IExternalApplication
{
public UIDocument ActiveUIDocument { get; private set; }
public Result OnStartup(UIControlledApplication application)
{
RibbonPanel ribbonPanel = application.CreateRibbonPanel("Tools");
string thisAssemblyPath = Assembly.GetExecutingAssembly().Location;
PushButtonData buttonData = new PushButtonData("cmdDeleteViews",
"Delete Views", thisAssemblyPath, "BGPanel.DeleteViews");
PushButton pushButton = ribbonPanel.AddItem(buttonData) as PushButton;
pushButton.ToolTip = "Delete all sheets, schedules & views except structural plans";
Uri uriImage = new Uri(#"C:\Revit_API\Revit_2015\32px-Broom.png");
BitmapImage largeImage = new BitmapImage(uriImage);
pushButton.LargeImage = largeImage;
return Result.Succeeded;
}
public void DeleteViews()
{
UIDocument uidoc = this.ActiveUIDocument;
Document doc = uidoc.Document;
FilteredElementCollector collector = new FilteredElementCollector(doc);
ICollection<Element> collection = collector.OfClass(typeof(View)).ToElements();
using (Transaction t = new Transaction(doc, "Delete Views"))
{
t.Start();
int x = 0;
foreach (Element e in collection)
{
try
{
View view = e as View;
switch (view.ViewType)
{
case ViewType.FloorPlan:
break;
case ViewType.EngineeringPlan:
break;
case ViewType.ThreeD:
break;
default:
doc.Delete(e.Id);
x += 1;
break;
}
}
catch (Exception ex)
{
View view = e as View;
TaskDialog.Show("Error", e.Name + "\n" + "\n" + ex.Message);
TaskDialog.Show("Error", ex.Message);
}
}
t.Commit();
TaskDialog.Show("BG_API DeleteViews", "Views Deleted: " + x.ToString());
}
}
public Result OnShutdown(UIControlledApplication application)
{
return Result.Succeeded;
}
}
}
First, instead typing the class name yourself, consider using Reflection like:
typeof(YourClassName).FullName
Second, every command in Revit requires its own class that implements IExternalCommand. I haven't tested, but your code should be something like the following:
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Media.Imaging;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace BGPanel
{
public class CsBGPanel : IExternalApplication
{
public Result OnStartup(UIControlledApplication application)
{
RibbonPanel ribbonPanel = application.CreateRibbonPanel("Tools");
string thisAssemblyPath = Assembly.GetExecutingAssembly().Location;
PushButtonData buttonData = new PushButtonData("cmdDeleteViews",
"Delete Views", thisAssemblyPath, typeof(DeleteViews).FullName);
PushButton pushButton = ribbonPanel.AddItem(buttonData) as PushButton;
pushButton.ToolTip = "Delete all sheets, schedules & views except structural plans";
Uri uriImage = new Uri(#"C:\Revit_API\Revit_2015\32px-Broom.png");
BitmapImage largeImage = new BitmapImage(uriImage);
pushButton.LargeImage = largeImage;
return Result.Succeeded;
}
public Result OnShutdown(UIControlledApplication application)
{
return Result.Succeeded;
}
}
public class DeleteViews : IExternalCommand
{
// this will not work...
//public UIDocument ActiveUIDocument { get; private set; }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument; //this.ActiveUIDocument;
Document doc = uidoc.Document;
FilteredElementCollector collector = new FilteredElementCollector(doc);
ICollection<Element> collection = collector.OfClass(typeof(View)).ToElements();
using (Transaction t = new Transaction(doc, "Delete Views"))
{
t.Start();
int x = 0;
foreach (Element e in collection)
{
try
{
View view = e as View;
switch (view.ViewType)
{
case ViewType.FloorPlan:
break;
case ViewType.EngineeringPlan:
break;
case ViewType.ThreeD:
break;
default:
doc.Delete(e.Id);
x += 1;
break;
}
}
catch (Exception ex)
{
View view = e as View;
TaskDialog.Show("Error", e.Name + "\n" + "\n" + ex.Message);
TaskDialog.Show("Error", ex.Message);
}
}
t.Commit();
TaskDialog.Show("BG_API DeleteViews", "Views Deleted: " + x.ToString());
}
return Result.Succeeded; // must return here
}
}
}
You should work through the Revit API getting started material before doing anything else at all, especially the DevTV and My First Revit Plugin video tutorials:
http://thebuildingcoder.typepad.com/blog/about-the-author.html#2
Then this question and many other fundamental issues will be answered up front, and you will save yourself and others some effort and head-scratching.
I needed to add a line before the IExternalCommand because of transaction errors when I clicked my button.
[Transaction(TransactionMode.Manual)]
The first answer to this question suggests using the reflection "typeof(YourClassName).FullName" When the sample code is tried it returns a error, understandably as Augusto says he did not test his example. Just adding for anyone down the road... If you use "typeof(DeleteViews).FullName" you would need to cast it into a string variable before the code in his example would work.

ProcessEvent not called in TFS plugin

Recently i have decided to create a plugin for the TFS for tracking work item changes based on ISubscriber interface.
So the workflow would be the following:
1) Work item state changes
2) Plugin catches the WorkItemChangedEvent
3) Send an email to the person specified in the Requester
As the base for my project, i used the following project from CodePlex - The Mail Alert
After i adopted it to my needs and saved compiled binaries in %TFS-DIR%\Microsoft Team Foundation Server 14.0\Application Tier\Web Services\bin\Plugins the TFS restarted the tier and... that's it. On work item change the ProcessEvent method is not called, but it should be.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.Framework.Server;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Server;
using System.Diagnostics;
using System.Xml.Linq;
using System.Xml;
using System.DirectoryServices;
using System.Net.Mail;
using System.Xml.Xsl;
using System.Configuration;
using System.Reflection;
namespace MailAlert
{
public class WorkItemChangedEventHandler : ISubscriber
{
static string serverPath = "";
static string ExternalURL = "";
static string MailAddressFrom = "";
static string SMTPHost = "";
static string Password = "";
static int Port = 25;
static int index = 0;
static string projectCollectionFolder;
static Uri projectCollectionUri;
static WorkItemStore wiStore;
static WorkItem wItem;
static WorkItemChangedEvent workItemChangedEvent;
static string teamProjectPath = "";
static VersionControlServer versionControlServer;
static TfsTeamProjectCollection projectCollection;
static Dictionary<IdentityDescriptor, TeamFoundationIdentity> m_identities = new Dictionary<IdentityDescriptor, TeamFoundationIdentity>(IdentityDescriptorComparer.Instance);
public Type[] SubscribedTypes()
{
return new Type[1] { typeof(WorkItemChangedEvent) };
}
public WorkItemChangedEventHandler()
{
TeamFoundationApplicationCore.Log("WorkItemChangedEvent Started", index++, EventLogEntryType.Information);
}
public EventNotificationStatus ProcessEvent(TeamFoundationRequestContext requestContext, NotificationType notificationType,
object notificationEventArgs, out int statusCode, out string statusMessage, out ExceptionPropertyCollection properties)
{
TeamFoundationApplicationCore.Log("WorkItemChangedEventHandler: ProcessEvent entered", index++, EventLogEntryType.Information);
statusCode = 0;
properties = null;
statusMessage = String.Empty;
GetTfsServerName();
projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(TfsTeamProjectCollection.GetFullyQualifiedUriForName(serverPath));
try
{
if (notificationType == NotificationType.Notification && notificationEventArgs is WorkItemChangedEvent)
{
workItemChangedEvent = notificationEventArgs as WorkItemChangedEvent;
TeamFoundationApplicationCore.Log("WorkItemChangedEventHandler: WorkItem " + workItemChangedEvent.WorkItemTitle + " was modified", index++, EventLogEntryType.Information);
TeamFoundationApplicationCore.Log("WorkItemChangedEventHandler: serverPath - " + serverPath, index++, EventLogEntryType.Information);
projectCollectionFolder = requestContext.ServiceHost.VirtualDirectory.ToString();
projectCollectionUri = new Uri(serverPath + projectCollectionFolder);
projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(projectCollectionUri);
wiStore = projectCollection.GetService<WorkItemStore>();
versionControlServer = projectCollection.GetService<VersionControlServer>();
TeamFoundationApplicationCore.Log("WorkItemChangedEventHandler: Before process workitem", index++, EventLogEntryType.Information);
ProcessWorkItem();
TeamFoundationApplicationCore.Log("WorkItemChangedEventHandler: After process workitem", index++, EventLogEntryType.Information);
}
}
catch (Exception ex)
{
TeamFoundationApplicationCore.Log("WorkItemChangedEventHandler: FUCKING EXCEPTION! =>\n" + ex.Message, index++, EventLogEntryType.Error);
}
return EventNotificationStatus.ActionPermitted;
}
private static void GetTfsServerName()
{
try
{
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(assemblyFolder + #"\Settings.xml");
// Declare the xpath for finding objects inside the XML file
XmlNodeList XmlDocNodes = XmlDoc.SelectNodes("/configuration/tfssettings");
XmlNodeList XmlDocExt = XmlDoc.SelectNodes("/configuration/Externaltfssettings");
// Define a new List, to store the objects we pull out of the XML
serverPath = XmlDocNodes[0].InnerText;
ExternalURL = XmlDocExt[0].InnerText;
XmlNodeList XmlDocNodes2 = XmlDoc.SelectNodes("/configuration/appSettings");
foreach (XmlNode mailNode in XmlDocNodes2)
{
foreach (XmlNode varElement in mailNode.ChildNodes)
{
switch (varElement.Attributes["key"].Value)
{
case "MailAddressFrom":
MailAddressFrom = varElement.Attributes["value"].Value;
break;
case "SMTPHost":
SMTPHost = varElement.Attributes["value"].Value;
break;
case "Password":
Password = varElement.Attributes["value"].Value;
break;
case "Port":
Port = Convert.ToInt32(varElement.Attributes["value"].Value);
break;
}
}
}
}
catch (Exception ex)
{
EventLog.WriteEntry("WorkItemChangedEventHandler", ex.Message);
}
}
public string Name
{
get { return "WorkItemChangedEventHandler"; }
}
public SubscriberPriority Priority
{
get { return SubscriberPriority.High; }
}
private static void ProcessWorkItem()
{
var teamProjects = versionControlServer.GetAllTeamProjects(false);
for (int i = 0; i < teamProjects.Length; i++)
{
string teamProjectName = teamProjects[i].Name;
var teamProject = teamProjects[i];
Project teamProjectWI = wiStore.Projects[i];
teamProjectPath = projectCollectionUri + teamProject.Name;
if (workItemChangedEvent.PortfolioProject == teamProjectName)
{
//get the workitem by ID ( CoreFields.IntegerFields[0] == ID ?!)
//check if any of String changed fields
foreach(StringField sf in workItemChangedEvent.ChangedFields.StringFields)
{
//is the State field
if (sf.Name.Equals("State"))
{
//then notify Reuqester
wItem = wiStore.GetWorkItem(workItemChangedEvent.CoreFields.IntegerFields[0].NewValue);
string CollGuid = projectCollection.InstanceId.ToString();
string Requester = wItem.Fields["Requester"].Value.ToString();
string WorkItemId = wItem.Id.ToString();
string mail = GetEmailAddress(Requester);
SendMail(CollGuid, WorkItemId, mail);
}
}
}
}
}
private static string GetEmailAddress(string userDisplayName)
{
DirectorySearcher ds = new DirectorySearcher();
ds.PropertiesToLoad.Add("mail");
ds.Filter = String.Format("(&(displayName={0})(objectCategory=person)((objectClass=user)))", userDisplayName);
SearchResultCollection results = ds.FindAll();
if (results.Count == 0)
{
return string.Empty;
}
ResultPropertyValueCollection values = results[0].Properties["mail"];
if (values.Count == 0)
{
return string.Empty;
}
return values[0].ToString();
}
private static void SendMail(string collID,string workItemId,string tomailAddrees)
{
MailMessage objeto_mail = new MailMessage();
SmtpClient client = new SmtpClient();
client.Port = Port;
client.Host = SMTPHost;
client.Timeout = 200000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = true;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential(MailAddressFrom, Password);
objeto_mail.From = new MailAddress(MailAddressFrom);
objeto_mail.To.Add(new MailAddress(tomailAddrees));
//objeto_mail.CC.Add(new MailAddress("nagarajb#hotmail.com"));
objeto_mail.Subject = "Work Item Changed:"+workItemId;
string mailbody = serverPath+"/tfs/web/wi.aspx?pcguid=" + collID + "&id=" + workItemId;
string mailbody2 = "";
if (ExternalURL.Length > 0)
{
mailbody2 = ExternalURL + "/tfs/web/wi.aspx?pcguid=" + collID + "&id=" + workItemId;
}
string tables = "<table border=1><tr><td>Work Item ID</td><td>" + wItem.Id.ToString() + "</td></tr><tr><td>Title</td><td>" + wItem.Title + "</td></tr><tr><td>State</td><td>" + wItem.State + "</td></tr><tr><td>Assigned To</td><td>" + wItem.Fields["Assigned to"].Value.ToString() + "</td></tr><tr><td>Internal URL</td><td>" + mailbody + "</td></tr><tr><td>External URL</td><td>" + mailbody2 + "</td></tr></table>";
objeto_mail.IsBodyHtml = true;
objeto_mail.Body = "<i>Hi " + wItem.Fields["Requester"].Value.ToString() + ","+"</i></br></br></br>" + tables + " </br></br> Best regards; </br></br>Configuration Management Team</br></br></br>";
client.Send(objeto_mail);
EventLog.WriteEntry("WorkItemChangedEventHandler", "Email Sent");
}
}
}
No errors or exceptions are thrown in the Event Log either. Tracing TFS (trace=true property in web.config) also was of no help.
Maybe someone could help or shed light on this mysterious case?
UPDATE:
Thanks for a reply Giulio Vian!
Here how it goes:
1) I haven't seen the dependencies broken, plus the constructor WorkItemChangedEventHandler is successfully called. That is seen in the Windows Event Log - WorkItemChangedEvent Started message is written.
2) I am not sure how to register the event handler.... i'll look that up
3) I am not sure how this works. I thought just copy-pasting dlls in the appropriate folder will do the trick, and there is no need for an account for the plugin. Mind giving a bit more info on this?
4) yes. Web.config in the main Web config in Application Tier\Web Services
5) Yes. Using an account with Administer rights. if i set a break point in the constructor, it is reached. Any other place in the code is not reached.
Many things can be wrong.
If any dependency is broken, you should see an error in the Event log
If you do not register the "WorkItemChangedEventHandler" event source, no message is written
Can the user account running the plugin access TFS?
How did you enabled the tracing (you pasted a bunch of code, but no configuration)?
Have you attached the debugger to the TFS process and set a breakpoint (do not this on a production TFS)?
We have collected similar suggestions in the documentation for our plugin at
https://github.com/tfsaggregator/tfsaggregator/wiki.

Sharepoint Client Object Model The property or field has not been initialized

I have a C# program that manages Sharepoint lists using the Sharepoint Client Object Model. Occasionally we will have server issues which will prevent the program from accessing the sharepoint server. I am using a helper class to run the ExecuteQuery method and have exception handling to continue to execute until there is no exception.
private void ExecuteContextQuery(ref ClientContext siteContext)
{
int timeOut = 10000;
int numberOfConnectionErrors = 0;
int maxNumberOfRetry = 60;
while (numberOfConnectionErrors < maxNumberOfRetry)
{
try
{
siteContext.ExecuteQuery();
break;
}
catch (Exception Ex)
{
numberOfConnectionErrors++;
Service.applicationLog.WriteLine("Unable to connect to the sharepoint site. Retrying in " + timeOut);
Service.applicationLog.WriteLine("Exception " + Ex.Message + " " + Ex.StackTrace);
System.Threading.Thread.Sleep(timeOut);
if (numberOfConnectionErrors == maxNumberOfRetry)
{
throw Ex;
}
}
}
}
However I getting an error messages
The property or field 'LoginName' has not been initialized.
and
collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
The error messages seem to be related to methods where I call the Load method. here is an example of my code that calls the method above.
List sharepointList = siteContext.Web.Lists.GetByTitle(this._listName);
CamlQuery query = CamlQuery.CreateAllItemsQuery();
items = sharepointList.GetItems(query);
siteContext.Load(items);
//siteContext.ExecuteQuery();
ExecuteContextQuery(ref siteContext);
Do I need to reload the site context with every call to ExecuteQuery? Is that why I am seeing the error message above?
Here is the function I am using for getting the Login ID which is generating the error
public String getLoginIDbyUserId(int userID)
{
ClientContext siteContext = getClientContextObject();
User _getUser = siteContext.Web.SiteUsers.GetById(userID);
siteContext.Load(_getUser);
//siteContext.ExecuteQuery();
ExecuteContextQuery(ref siteContext);
String loginID = String.Empty;
String formatedLoginID = String.Empty;
loginID = _getUser.LoginName;
if (loginID.Contains('|'))
{
formatedLoginID = loginID.Substring(loginID.IndexOf('|') + 1);
}
siteContext.Dispose();
return formatedLoginID;
}
Please try to load LoginName property of user while loading user object. And after excutequery method, try to consume LoginName property of User
siteContext.Load(_getUser, u => u.LoginName);
And after this change your code should look like this
public String getLoginIDbyUserId(int userID)
{
ClientContext siteContext = getClientContextObject();
User _getUser = siteContext.Web.SiteUsers.GetById(userID);
siteContext.Load(_getUser, u => u.LoginName);
//siteContext.ExecuteQuery();
ExecuteContextQuery(ref siteContext);
String loginID = String.Empty;
String formatedLoginID = String.Empty;
loginID = _getUser.LoginName;
if (loginID.Contains('|'))
{
formatedLoginID = loginID.Substring(loginID.IndexOf('|') + 1);
}
siteContext.Dispose();
return formatedLoginID;
}

Categories