Thursday, September 29, 2011

search SharePoint list and return matching column using LINQ

I Have writen this reusable code, which will open a Sharepoint List, and search that list and return the first matching row's specific column:
This function need 04 parameters: ListName, Search FieldName, Search Value, and return Column name.


public string GetAColumnValueFromList(string listName, string Field, string value, string returnField)
{
string outData = string.Empty;
DataTable dtResult = new DataTable();

try
{
if (oList == null)
{
using (SPSite oSiteCollection = new SPSite(siteUrl))
{
using (SPWeb oWebsiteRoot = oSiteCollection.OpenWeb("/"))
{
oList = oWebsiteRoot.Lists[listName];

}
}
}

List items = (from li in oList.Items.OfType()
where li["GUID1"].ToString() == value
select li).ToList();

foreach (SPListItem item in items)
{
outData = item[returnField].ToString();
}

return outData;
}
catch (Exception ex)
{
EnterpriseLibrary.HandleException(ex, Policies.BLL);
throw ex;
}
}


If you like this solution, then care for a tree which need some water, so give water to a plant and make that green.
Start using http://www.code4green.com as your free code generation tool for Sharepoint, PowerShell, C#, ASP.net, SQL, and many more languages.
let other know about this tool, because this tool is helping community.

LINQ Query on SPListItem returning "Value does not fall within the expected range."

If you are doing LINQ Qery on SHarepoint SpListItem object and getting the following error: "Value does not fall within the expected range."
Might be this little simple (addition of .ToString()) will help you to make it work.
Original Code
List items = (from li in oList.Items.OfType()
where li["UserName"] == value
select li).ToList();

change this query to like this by adding .ToString() in li["ColName"]=="Value"
Changed Code
List items = (from li in oList.Items.OfType()
where li["Username"].ToString() == value
select li).ToList();

If you like this solution, then care for a tree which needs some water, so water a plant and make that green.
Start using http://www.code4green.com as your free code generation tool for Sharepoint, PowerShell, C#, ASP.net, SQL, and many more languages.
let other know about this tool, because this tool is helping community to plant tree.

Monday, September 19, 2011

Windows service is not able to read outlook email

Problem: image created by code4green.com If you are trying to read the outlook email (using outlook api) from your "windows service", then it will not show any email.
Reason: Your service is running under Local System Account, which has no profile in your outlook.
Your service will runn success full but it will not show any email because of not-matched user profile.
Solution: Change the service account "Log On" option, so your windows service will run under a user account which has user profile for outlook.
Steps: Here are the steps to fix this issue -
1. Right click on My Computer, click on Manage
2. Clicke on Service, and select your service like "Code4Green Email Reader"
3. Click on service properties
4. Click the tab Log On
5. select Logon as: This account -
6. enter outlook's profile login name with domain name
7. enter password
8. enter confirm password
9. click Apply
10. click OK



Friday, September 16, 2011

Remove the dll from GAC without using GAC util

Using the following name space :System.EnterpriseServices.Internal, you can uninstall or remove assembly from GAC without using GacUril in your production environment.

I have seen on web, that people are getting hard time to use Publish.GacRemove method, because they don't know how to give the path name.
Here are two step to uninstall the dll from GAC without using GacUtil.

STEP 1:

Copy the existing dll into a temp folder and then use that path to remove/uninstall. Here is the Powershell code to copy the dll from gac to directory:
dir C:\Windows\Assembly -Recurse -Filter "YourDLLName*.dll" foreach { copy $_.FullName C:\Temp\assemblies }

STEP 2:
Here is the powershell code to remove the dll from gac using path in step1

# load System.EnterpriseServices assembly
[Reflection.Assembly]::LoadWithPartialName("System.EnterpriseServices, Version=2.0.0.0")

# create an instance of publish class
[System.EnterpriseServices.Internal.Publish] $publish = new-object System.EnterpriseServices.Internal.Publish

#UnInstall UserTracking.DAL.dll in gac
$AssemblyFullName = "C:\Temp\Assembly\YourDLLName*.dll"
$publish.GacRemove($AssemblyFullName)

dll name in $AssemblyFullName = "C:\Temp\Assembly\YourDLLName*.dll", is copied from GAC to C:\Temp\Assembly folder in step1.

I hope this article will help to someone, please plant a tree in your life span if you get any help from this as a donation to www.code4green.com