Global service of the Team explorer's Query results window - c#

What is the Query results window's global service (interface)? Code below:
var dteService = Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
if (dteService == null)
{
Debug.WriteLine("");
return;
}
var something=Package.GetGlobalService(typeof(???)) as ???;
EDIT: The goal is, when I press the context menu button, I want the function callback to be able to access the service where the work item is selected (or the results list

Please check this case in MSDN forum for the details how to get it work: https://social.msdn.microsoft.com/Forums/vstudio/en-US/2d158b9c-dec1-4c59-82aa-f1f2312d770b/sdk-packageget-selected-item-from-query-results-list
The following code is quoted from above link for your quick reference:
Document activeDocument = _applicationObject.ActiveDocument;
if (activeDocument != null)
{
DocumentService globalService = (DocumentService)Package.GetGlobalService(typeof(DocumentService));
if (globalService != null)
{
string fullName = activeDocument.FullName;
IWorkItemTrackingDocument document2 = globalService.FindDocument(fullName, null);
if ((document2 != null) && (document2 is IResultsDocument))
{
int[] selectedItemIds = ((IResultsDocument)document2).SelectedItemIds;
}
}
}

var dteService = Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
if (dteService == null)
{
Debug.WriteLine("");
return;
}
DocumentService documentService = Package.GetGlobalService(typeof(DocumentService)) as DocumentService;
if (documentService == null)
return;
string fullName = dteService.ActiveDocument.FullName;
IWorkItemTrackingDocument activeDocument = documentService.FindDocument(fullName, null);
if (activeDocument == null || !(activeDocument is IResultsDocument))
return;

Related

Getting the active url of browser in C# windows form application

I have created a window form application.
This App get the active url of browser and save this into the text file.
And this works fine in chrome & IE.
But when i use firefox, this will not work. This code fails to get the active url of firefox browser.
I don't know why this happening.
I am using the following code to find the URL
public string GetBrowsedUrl()
{
IntPtr hwnd = APIFuncs.getforegroundWindow();
Int32 pid = APIFuncs.GetWindowProcessID(hwnd);
Process process = Process.GetProcessById(pid);
string appId = proc.Id.ToString();
string appName = proc.ProcessName;
string appltitle = APIFuncs.ActiveApplTitle().Trim().Replace("\0", "");
if (process == null)
throw new ArgumentNullException("process");
if (process.MainWindowHandle == IntPtr.Zero)
return null;
AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
if (element == null)
return null;
AutomationElement edit = element.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
string result = ((ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
return result;
}
Finally i found the answer
public string GetBrowsedUrl(Process process)
{
if (process.ProcessName == "firefox")
{
if (process == null)
throw new ArgumentNullException("process");
if (process.MainWindowHandle == IntPtr.Zero)
return null;
AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
if (element == null)
return null;
AutomationElement doc = element.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document));
if (doc == null)
return null;
return ((ValuePattern)doc.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
}
else
{
if (process == null)
throw new ArgumentNullException("process");
if (process.MainWindowHandle == IntPtr.Zero)
return null;
AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
if (element == null)
return null;
AutomationElement edit = element.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
string result = ((ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
return result;
}
}
You can not use this code for firefox too.
I recommend a third party library named NDde to do this very easily.
Here is NDde link
public string GetFirefoxUrl()
{
try
{
Process[] pname = Process.GetProcessesByName("Firefox");
if (pname.Length != 0)
{
DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
dde.Connect();
string url = dde.Request("URL", int.MaxValue);
url= url.Replace("\"", "").Replace("\0", "");
dde.Disconnect();
return url;
}
else
return null;
}
catch
{
return null;
}
}

getting error while fetching fields from Dynamics CRM(Online Sales) from different server location

I am trying to fetch all account entity fields from an organization.(https://democrm365.crm4.dynamics.com).
Also, I created some custom fields in account and added into form. After that when I run the following code I am getting all fields related to account.
IOrganizationService service = (IOrganizationService)serviceProxy;
RetrieveEntityRequest request = new RetrieveEntityRequest()
{
EntityFilters = EntityFilters.Attributes,
RetrieveAsIfPublished = false,
LogicalName = "account"
};
RetrieveEntityResponse res = (RetrieveEntityResponse)service.Execute(request);
EntityMetadata currentEntity = res.EntityMetadata;
foreach (AttributeMetadata attribute in currentEntity.Attributes)
{
if (attribute.DisplayName.UserLocalizedLabel != null && attribute.AttributeType != null && attribute.LogicalName != "" && attribute.AttributeType != null && attribute.IsValidForRead.Value == true)
{
if (_allowedFieldTypes.Contains(attribute.AttributeType.ToString().ToLower()))
{
EntityField ef = new EntityField();
ef.AttributeType = attribute.AttributeType.ToString() ?? "";
ef.DisplayName = attribute.DisplayName.UserLocalizedLabel.Label;
ef.IsCustomField = attribute.IsCustomAttribute ?? false;
ef.IsAllowUpdate = attribute.IsValidForUpdate ?? false;
ef.LogicalName = attribute.LogicalName;
if (attribute.AttributeType.ToString().ToLower() == "picklist")
{
PicklistAttributeMetadata pm = (PicklistAttributeMetadata)attribute;
foreach (OptionMetadata x in pm.OptionSet.Options)
{
ef.Items.Add(x.Label.UserLocalizedLabel.Label);
}
}
else if (attribute.AttributeType.ToString().ToLower() == "virtual")
{
if (attribute.AttributeTypeName.Value == "MultiSelectPicklistType")
{
MultiSelectPicklistAttributeMetadata pm = (MultiSelectPicklistAttributeMetadata)attribute;
foreach (OptionMetadata x in pm.OptionSet.Options)
{
ef.Items.Add(x.Label.UserLocalizedLabel.Label);
}
}
}
if (Add)
{
fieldLst.Add(ef);
}
}
}
}
Again, I tested the same code on different organization (https://zoho5.crm.dynamics.com) with all above mentioned steps, then
below code is not working.
if (attribute.DisplayName.UserLocalizedLabel != null && attribute.AttributeType != null && attribute.LogicalName != "" && attribute.AttributeType != null && attribute.IsValidForRead.Value == true)
attribute.DisplayName.UserLocalizedLabel is null for all fields like (Account Name, Account No. Etc)
After some test runs, I removed the custom fields from account form and publish form. Then the above code is working fine.
Try this. You may have to get the label from LocalizedLabels when UserLocalizedLabel is null.
foreach (AttributeMetadata attribute in currentEntity.Attributes)
{
if (attribute.AttributeType != null && attribute.LogicalName != "" && attribute.AttributeType != null && attribute.IsValidForRead.Value == true)
{
string attributeName = attribute.LogicalName;
if (attribute.DisplayName.UserLocalizedLabel != null)
{
attributeName = attribute.DisplayName.UserLocalizedLabel.Label;
}
if (attributeName == attribute.LogicalName && attribute.DisplayName.LocalizedLabels.Count > 0)
{
attributeName = attribute.DisplayName.LocalizedLabels[0].Label;
}
}
}

How to get the sender e-mail address associated by the folder item? (vsto / outlook 2010 / mapi)

I have in my Outlook 2010-Add-In (c#) many folders. They are in my private post box or in one of my shared post boxes.
Now I am looking for a solution to find out, how to get the right email address (sender / recipient) associated with a dedicated folder. It could be any folder from my private or anyone of my shared post boxes.
I think, maybe I could use the EntryId / StoreId from the folder item to identify the corresponding email address.
I know already, that I could get the email address from any mail item but I'm not looking for this solution.
I like to answer my own questions: I think that I've found a plausible solution.
I do not treat any exceptions inside the function, I do that from outside.
private string GetSMTPAddressByFolderItem(Outlook.MAPIFolder mapiFolder)
{
string PR_MAILBOX_OWNER_ENTRYID = #"http://schemas.microsoft.com/mapi/proptag/0x661B0102";
string PR_SMTP_ADDRESS = #"http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
Outlook.Store store = null;
Outlook.NameSpace ns = null;
Outlook.AddressEntry sender = null;
Outlook._ExchangeUser exchUser = null;
try
{
if (mapiFolder == null)
{
return null;
}
// Get the parent store.
store = mapiFolder.Store;
string storeOwnerEntryId = store.PropertyAccessor.BinaryToString(store.PropertyAccessor.GetProperty(PR_MAILBOX_OWNER_ENTRYID)) as string;
ns = Application.GetNamespace(Constants.OL_NAMESPACE); // i.e. "MAPI"
sender = ns.GetAddressEntryFromID(storeOwnerEntryId);
if (sender != null)
{
if (sender.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry ||
sender.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry)
{
exchUser = sender.GetExchangeUser();
if (exchUser != null)
{
return exchUser.PrimarySmtpAddress;
}
else
{
return null;
}
}
else
{
return sender.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS) as string;
}
}
return null;
}
finally
{
if (ns != null)
{
Marshal.ReleaseComObject(ns);
ns = null;
}
if (store != null)
{
Marshal.ReleaseComObject(store);
store = null;
}
if (sender != null)
{
Marshal.ReleaseComObject(sender);
sender = null;
}
if (exchUser != null)
{
Marshal.ReleaseComObject(exchUser);
exchUser = null;
}
}
}

C# Error : NullReferenceException was unhandled by user code

I am developing a payroll add-on for SAP business 1. I keep getting an error:
"NullReferenceException was unhandled by user code :Object reference not set to an instance of an object." When I try to select a combobox item that is embedded in a SAP matrix column cell.
My Code:
public void HandleMenuEvent(ref SAPbouiCOM.MenuEvent pVal)
{
// Handle Add Menu
if (pVal.MenuUID == "1282")
{
_form.Freeze(true);
oMatrix.AddRow();
_edCode.ValueEx = string.Empty;
_cmbEDDescription = oMatrix.Columns.Item("EDDesc").Cells.Item(oMatrix.RowCount).Specific;
var earnDeductDescription = Program.Kernel.Get().GetAllEarnDeductMasters().Distinct();
if (_cmbEDDescription.ValidValues.Count > 0)
{
// Do nothing
}
else
{
foreach (var item in earnDeductDescription)
{
_cmbEDDescription.ValidValues.Add(item.U_PD_description, string.Empty);
}
}
_cmbEDDescription.Select(0, SAPbouiCOM.BoSearchKey.psk_Index);
var edDescValue = string.Empty;
edDescValue = _cmbEDDescription.Value;
var edCode = earnDeductDescription.Where(x => x.U_PD_description.Trim() == edDescValue.Trim()).Select(y => y.U_PD_code).SingleOrDefault();
for (int i = 1; i
The error occurs on the item changed event
#region ItemChanged
if (pVal.ItemChanged && pVal.ColUID == "EDDesc" && pVal.Before_Action == false)
{
var earnDeductDescription = Program.Kernel.Get().GetAllEarnDeductMasters().Distinct();
var edDescValue = string.Empty;
edDescValue = _cmbEDDescription.Selected.Value; x.U_PD_description.Trim() == edDescValue.Trim()).Select(y => y.U_PD_code).SingleOrDefault();
for (int i = 1; i
This is where I attach a user data source to the SAP column
private void BindMatrixToUserDataSource()
{
// Get main matrix
oItem = _form.Items.Item("JournalMat");
oMatrix = oItem.Specific;
_edDescription = _form.DataSources.UserDataSources.Add("EDDesc", SAPbouiCOM.BoDataType.dt_SHORT_TEXT, 30);
oColumns = oMatrix.Columns;
_coledDescription = oColumns.Item("EDDesc");
_coledDescription.DataBind.SetBound(true, "", "EDDesc");
...some code
}
Can anyone help me solve this?
My suggestion is that _cmbEDDescription.Selected is null at that moment, because no item is selected in the ComboBox. You might change your code like that:
var edDescValue = _cmbEDDescription.Selected == null ? string.Empty : _cmbEDDescription.Selected.Value;

How do i Return null or matching xml node as Object in C#

I got a xml structure as below:
<Users>
<User Code="1" Roles="1,2,3" />
</Users>
I provide a method to search the xml file for retrieving particular user based on code like below
string xpath = "Users/User[#Code="+ Code +"]";
XmlNode user = _xmlDatabase.SelectSingleNode(xpath);
if (user != null)
{
XmlAttributeCollection userMeta = user.Attributes;
if (userMeta != null)
{
int code = int.Parse(Code);
User userInstance = new User(Code, userMeta[1].Value, userMeta[2].Value);
return userInstance;
}
}
i would invoke the method like so
User user = GetUserByCode("1"); & _xmlDatabase is a instance of XmlDocument class. Here is the question,
I get to return null when no matching user is found
Attributes i search for does not exists
It's a fresh file
Hence i modified the method to return "null"only to be complained by compiler that "return statement is missing"
I kind of wanted the end-user to do
User user = GetUserByCode("1");
if(user == null)
Display "No User Found"
please see the comments on below code
if (user != null) // if user == null nothing will return
{
XmlAttributeCollection userMeta = user.Attributes;
if (userMeta != null) // if userMeta == null nothing will return
{
int code = int.Parse(Code);
User userInstance = new User(Code, userMeta[1].Value, userMeta[2].Value);
return userInstance;
}
}
you can solve this as below
public User GetUserByCode(string Code)
{
User userInstance = null;
string xpath = "Users/User[#Code="+ Code +"]";
XmlNode user = _xmlDatabase.SelectSingleNode(xpath);
if (user != null)
{
XmlAttributeCollection userMeta = user.Attributes;
if (userMeta != null)
{
int code = int.Parse(Code);
userInstance = new User(Code, userMeta[1].Value, userMeta[2].Value);
}
}
return userInstance;
}
Above code will return null or userInstance in any case.

Categories

Resources