Wednesday, December 26, 2012

How to generate pdb file for project?

A program database (PDB) file holds debugging and project state information that allows incremental linking of a Debug configuration of your program.

If debugging is enabled but if project not generating pdb file which contains the debug configuration then it will not hit the debug point.
- To enable creating pdb file for project follow the steps given below(below screenshot Step 1).
(1) Right Click on Project and Click on Properties.
(2) Select Build Option
(3) At bottom, click on “Advanced…” button and it will open up with popup “Advanced Build Settings
(4) Under “Output” section, select “full” from dropdownlist of “Debug Info”
(5) Click on OK button
(6) Now, clean your solution and rebuild solution

Step 1

If above step not works for your or you couldn’t find location specified then follow steps given below (below screenshot Step 2).
(1) Right Click on Project and Click on Properties.
(2) Select Build Compile
(3) At bottom, click on “Advanced Compile Options…” button and it will open up with popup “Advanced Compile Settings
(4) Under “Optimizations” section, select “full” from dropdownlist of “Generate Debug Info
(5) Click on OK button
(6) Now, clean your solution and rebuild solution
 
Step 2

It will generate new pdb file for the project.
- That’s it for creating pdb file for project.

Friday, September 7, 2012

How to create folder in sharepoint custom list programmatically


Here is steps to add new item as a folder in sharepoint list programmatically.

- Create new instance of site with your current application
using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
{

- Create new web instance and open web for current application
using (SPWeb webFolder = site.OpenWeb())
{

- Get folder by folder name in list

SPFolder folder = webFolder.GetFolder("/Lists/Your List Name/Your Folder Name");

- Check whether folder is exist in list or not, if not then create new folder
if (folder.Exists)
{
Response.Write("Folder name already exists.");
return;
}

- Create list and Get County list
SPList list = webFolder.Lists[Your List Name];

- Set AllowUnsafeUpdates to true to insert new item through code
webFolder.AllowUnsafeUpdates = true;

- Create new item to add
SPListItem item = list.Items.Add();
item.FileSystemObjectType = SPFileSystemObjectType.Folder;

- Set values for the folder name
item["Title"] = “Your Folder Name”;

- fire update event for County list
item.Update();
webFolder.Update();

 }
}

-That’s it for adding new item to your sharepoint list as folder.

Saturday, June 30, 2012

Delete sharepoint list item attachment programmatically




Here is steps to delete sharepoint list item attachment programmatically.

- Follow the steps to delete list item attachment from your sharepoint list.

- Create new instance of site with your current application
using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
{

- Create new web instance and open web for current application
using (SPWeb web = site.OpenWeb())
{

- Create new list object and get your list
SPList SPLData = web.Lists["Your List Name"];

- Create new query object to hold query which fetches list item of the list
SPQuery SPMyQuery = new SPQuery();

- Assign query with value to it in ID field of the list item

SPMyQuery.Query = string.Format(“<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>{0}</Value></Eq></Where>”,1);

//========== Fetch item collection data for Your List  ==========//
SPListItemCollection common = SPLData.GetItems(SPMyQuery);

SPListItem listItem = common[0];

if (listItem.Attachments.Count > 0)
{
listItem.Attachments.Delete(“Name of file”);
listItem.Update();
}

}

}

-That’s it for deleting attachments from your existing list item of sharepoint list.

Wednesday, June 13, 2012

Get sharepoint list item attachment programmatically


Here is steps to get sharepoint list item attachment programmatically.

- Follow the steps to insert new item into your sharepoint list.

- Create new instance of site with your current application
using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
{

- Create new web instance and open web for current application
using (SPWeb web = site.OpenWeb())
{

- Create new list object and get your list
SPList SPLData = web.Lists["Your List Name"];

- Create new query object to hold query which fetches list item of the list
SPQuery SPMyQuery = new SPQuery();

- Assign query with value to it in ID field of the list item
SPMyQuery.Query = string.Format(“<Where
>  < Eq >  < FieldRef Name='ID' / >  < Value Type='Counter' > {0} < /Value >  < /Eq >  < /Where >”,1);

- Assign query to list and get item on the basis of the query build above
SPListItemCollection common = SPLData.GetItems(SPMyQuery);

- Check whether list collection contains result of the query, if contains item the proceed to update value of the list item
if (common.Count > 0)
{

SPListItem mylistItem = common[0];
string[] _Str_Attachments = GetAttachmentCollection(mylistItem);

- You can assign attachment collection with grid and if you want to give link to the attachment filename, on each file name with anchor link put following code for href of anchor.
string.Format("/Lists/{0}/Attachments/{1}/{2}", "Your List Name”,1,”FileName”);

}
}
}

- Get sharepoint list item attachment collection
public string[] GetAttachmentCollection(SPListItem item)
{
string FileNames = String.Empty;
if (item.Attachments.Count > 0)
{
string[] _Str_File_Name = new string[item.Attachments.Count];
for (int i = 0; i < item.Attachments.Count; i++)
{
SPFile file = item.ParentList.ParentWeb.GetFile(item.Attachments.UrlPrefix + item.Attachments[i].ToString());
_Str_File_Name[i]= file.Name;
}
return _Str_File_Name;
}
return null;
}



-That’s it for fetching attachments for existing item of sharepoint list.

Saturday, June 2, 2012

Insert new item with attachment to sharepoint list programmatically


Here is steps to add new item with attachment to sharepoint list programmatically.

- Get name of the file
string _Str_File_Name = fuAttachment.PostedFile.FileName;

- Get stream data by following way
byte[] contents = GetFileStream();

- Declare variable which hold the newly created item id.
int _Int_ID = 0;

- Follow the steps to insert new item into your sharepoint list with attachment.

- Create new instance of site with your current application
using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
{

- Create new web instance and open web for current application
using (SPWeb web = site.OpenWeb())
{

- Create new list object and get your list
SPList list = web.Lists["Your List Name"];

- Set allowunsafeupdates for web & site to true
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;

- Create new list item object and add new item for newly created list object
SPListItem item = list.Items.Add();

- Assign value for newly created item
item["Title"] = "Title1";

- Assign selected filename and stream data
if (!string.IsNullOrEmpty(_Str_File_Name != null) && (contents.Length > 0))
{
SPAttachmentCollection fileAttach = item.Attachments;
fileAttach.Add(_Str_File_Name, contents );
}


- Update item which will add new item to list
item.Update();

- if you want to manipulate on newly created item then you can get id of the newly created item by following way
itemId = item.ID;
}
}

- Convert your selected file to byte by following function
private byte[] GetFileStream()
{
//fuAttachment is your file select control
if (!fuAttachment.HasFile)
return null;
Stream fStream = fuAttachment.PostedFile.InputStream;
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
return contents;
}
Click Insert new item to sharepoint list programmatically to view my previous post to add item to sharepoint list.

-That’s it for adding new item to your sharepoint list with attachment.

Thursday, May 31, 2012

Delete sharepoint list item programmatically

- Create new instance of site with your current application
using (SPSite site = new SPSite(SPContext.Current.Web.ToString())) 
{

- Create new web instance and open web for current application
using (SPWeb web = site.OpenWeb())
{
- Create new list object and get your list
SPList SPLData = web.Lists["Your List Name"];

- Create new query object to hold query which fetches list item of the list
SPQuery SPMyQuery = new SPQuery();

- Assign query with value to it in ID field of the list item
SPMyQuery.Query = string.Format(“<Where><Eq><FieldRef Name='ID' /><Value Type='Counter' >{0}</Value> </Eq> </Where>”,1);

- Assign query to list and get item on the basis of the query build above
SPListItemCollection common = SPLData.GetItems(SPMyQuery);

- Check whether list collection contains result of the query, if contains item the proceed to update value of the list item
if (common.Count > 0)
{

- Set allowunsafeupdates for web to true
web.AllowUnsafeUpdates = true;

- Delete selected item from fetched list
common[0].Delete();

- Update item which will add new item to list
web.Update();

}
}


Tuesday, May 29, 2012

Update existing sharepoint list item programmatically


Here is steps to update existing sharepoint list item programmatically.

- Follow the steps to insert new item into your sharepoint list.

- Create new instance of site with your current application
using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
{

- Create new web instance and open web for current application
using (SPWeb web = site.OpenWeb())
{

- Create new list object and get your list
SPList SPLData = web.Lists["Your List Name"];

- Create new query object to hold query which fetches list item of the list
SPQuery SPMyQuery = new SPQuery();

- Assign query with value to it in ID field of the list item
SPMyQuery.Query = string.Format(“
< Where >  < Eq >  < FieldRef Name='ID' / >  < Value Type='Counter' > {0} < /Value >  < /Eq >  < /Where > ”,1); 

- Assign query to list and get item on the basis of the query build above
SPListItemCollection common = SPLData.GetItems(SPMyQuery);

- Check whether list collection contains result of the query, if contains item the proceed to update value of the list item
if (common.Count > 0)
{

- Set allowunsafeupdates for web to true
web.AllowUnsafeUpdates = true;


- Get first list item from the result list
SPListItem item = common[0];

- Set new value for the column which we want to update
item["Title"] = “New Value”;

- Update item which will add new item to list
item.Update();
}
}
}

-That’s it for updating existing item of sharepoint list.

Monday, May 28, 2012

Insert new item to sharepoint list programmatically


Here is steps to add new item to sharepoint list programmatically.
- Declare variable which hold the newly created item id.
int _Int_ID = 0;

- Follow the steps to insert new item into your sharepoint list.


- Create new instance of site with your current application
using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
{

- Create new web instance and open web for current application
        using (SPWeb web = site.OpenWeb())
        {

- Create new list object and get your list
                SPList list = web.Lists["Your List Name"];

- Set allowunsafeupdates for web & site to true
                site.AllowUnsafeUpdates = true;
                web.AllowUnsafeUpdates = true;

- Create new list item object and add new item for newly created list object
                SPListItem item = list.Items.Add();
- Assign value for newly created item
                item["Title"] = "Title1";
- Update item which will add new item to list
                item.Update();
- if you want to manipulate on newly created item then you can get id of the newly created item by following way
                itemId = item.ID;
        }
}

-That’s it for adding new item to your sharepoint list.

Thursday, May 17, 2012

How to Create New Sharepoint List Programmatically

Creation of new list includes following things.
- Create new list
- Create new field to newly created list
- Create new field which will set default value when new item created
- Show newly created field in view list of list
- Create lookup column to newly created list
- If list is already exist then create column to it


Here is the steps to create product list and columns in it.
(1) First, get list with name “Product” and check whether list is not null.

SPList ListProduct = web.Lists.TryGetList(“Product”);
if (ListProduct == null)
{

(2) if “Product” list is null then following step will create new list for sharepoint
web.Lists.Add(“Product”, "Product List Instance", SPListTemplateType.GenericList);
web.Update();

(3) After Creation of list “Product”, again, Get newly create list “Product”
SPList ProductList = web.Lists[“Product”];

(4) if you want to set product list to be appear on quick launch bar of sharepoint site then make it true/false accordingly
ProductList.OnQuickLaunch = false;

(5) Create field with name “Active”, if you want to add default value for field this way you can add default value

SPFieldBoolean fldActive = (SPFieldBoolean)ProductList.Fields.CreateNewField(SPFieldType.Boolean.ToString(), "Active");
fldActive.DefaultValue = "1";
ProductList.Fields.Add(fldActive);

(6) Simply, if you want to add field and there is no need to put any default value on the time of creation of new item you can add field by following way.
ProductList.Fields.Add("Description", SPFieldType.Note, false);

(7) After adding fields to List, update list.
ProductList.Update();

(8) After creation of list, then adding new fields to list, if you want to add newly added fields to view list of list then call following function.
CommonMethods.SetFieldInViewField(ProductList, "Active");

CommonMethods.SetFieldInViewField(ProductList, "Description");
(9) from above code we have added those column which are simple to add, but if you want to add any lookup column to list then use following step.
- Get list which you want to use as reference to lookup column
SPList ListCategory = web.Lists.TryGetList(“Category”);

- Call following function which will add new column named “Category” to “Product” list and adds reference of list “Category” with its field “Title”
CommonMethods.CreateLookupColumn(ProductList , ListCategory, "Category", "Title");

(10) Again, after creation of field, just put “Category” field to view list of list

//========== put field in view fields ==========//
CommonMethods.SetFieldInViewField(ProductList , "Category");
}

- To Set field in view list of list, use following function
public static void SetFieldInViewField(SPList ListName, string _Str_Column_Name)
{
SPView oView = ListName.Views["All Items"];
SPViewFieldCollection collViewFields = oView.ViewFields;
collViewFields.Add(_Str_Column_Name);
oView.Update();
}

- To add lookup column, use following function
public static void CreateLookupColumn(SPList Lst_Source_List, SPList Lst_Lookup_List, string _Str_Target_Column_Name, string _Str_Lookup_Column_Name)
{
SPList ListTest = Lst_Source_List;
ListTest.Fields.AddLookup(_Str_Target_Column_Name, Lst_Lookup_List.ID, false);
SPFieldLookup lkp = (SPFieldLookup)ListTest.Fields[_Str_Target_Column_Name];
lkp.LookupField = Lst_Lookup_List.Fields[_Str_Lookup_Column_Name].InternalName;
lkp.Update();
}

- To create new column when list is exist follow steps given below.
(1) SPList ListProduct = web.Lists.TryGetList(“Product”);
(2) if(ListProduct != null)
{
- Check whether the column with name “SortOrder” is not exist then add new column
if (!ListProduct .Fields.ContainsFieldWithStaticName("SortOrder"))
{
- Add new column and update list
ListProduct .Fields.Add("SortOrder", SPFieldType.Number, false);
ListProduct .Update();
- Add newly created column to view list of the list

//========== put field in view fields ==========//
CommonMethods.SetFieldInViewField(ListProduct , "SortOrder");
}
}

so, that’s it for all. 

Tuesday, May 15, 2012

How to remove your Outlook Add-In from Outlook

Here is the steps how to remove your Outlook Add-In from your Outlook 2007/2010

(A) Outlook 2010

(1) File –> Options it will open up with Options Dialog shown below in screenshot.
Select “Add-Ins” from Left Hand side of Options Dialog
(2) From the above screenshot at the bottom, select “Go” button, and it will open up with dialog shown below in screenshot.


(3) Select "My First Add-In Friendly Name" and click on "Remove" button, it will automatically remove your Outlook Add-In from your Outlook.

(B) Outlook 2007

(1) File –> Tools --> Trust Center, it will open up with Trust Center Dialog shown below in screenshot.
Select “Add-Ins” from Left Hand side of  Trust Center Dialog


(2) From the above screenshot at the bottom, select “Go” button, and it will open up with dialog shown below in screenshot.
(3) Select "My First Add-In Friendly Name" and click on "Remove" button, it will automatically remove your Outlook Add-In from your Outlook.



Create your first setup project for Outlook Add-in


Here is the steps which will show you how to create your first outlook add-in and setup project for same Outlook Add-in.

Please follow steps given below to create your first Outlook Add-in which will be installable in your Outlook 2010.


(1) Create new project for Outlook Add-in by navigating
File –> New –> Project –> Visual C# –> Office –> 2010 –> Outlook 2010 Add-in –> Give it any name “MyFirstAddIn” and click OK.

(2) To work with code, just put following line of code in auto generated event “ThisAddIn_Startup”
“MessageBox.Show("hi");”
Add namespace “using System.Windows.Forms;” for above code
After putting this code it will look like following screenshot.

(3) Now, to create Setup project for above Outlook Add-In follow steps given below in same project solution.

(4) Right Click on solution –> Add –> New Project –> Other Project Types –> Setup and Deployment –> Visual Studio Installer –> Setup Project –> Give it any name “MyFirstSetup” and click OK.

(5) Right Click on “MyFirstSetup” –> View –> File System. It will open up with File System area with following options on Left Hand Panel. (1) Application Folder (2) User’s Desktop (3) User’s Program Menu

(6) Right Click on “Application Folder” –> Add –> Project Output and it will open up with following screenshot.

Select your Add-in project and select Primary Output then click on OK.
It will selects all dlls and runtime files needed for this output in Right Hand Side panel. Again, Right Click on “Application Folder” –> Add –> File and it will open with “Add Files” dialog, navigate to your Add-in project’s bin folder then select Debug folder and select “MyFirstAddIn.vsto” & “MyFirstAddIn.dll.manifest” file and click OPEN. Again, you will find this two files were added to Right Hand side panel.

- To add Manufacturer path for this installation Right Click on “Application Folder” –> Properties Window –> Default Location and put value “[AppDataFolder][Manufacturer]\[ProductName]” in Default Lcoation.



(7) Right Click on “MyFirstSetup” –> View –> Registry. It will open up with Registry area with following options on Left Hand Panel shown below in screenshot.


(8) From this pane, Expand HKEY_CURRENT_USER –> Software. Follow the steps given below to add your registry entry.

- Under Software there will be default “[Manufacturer]” key is added, rename it with “Microsoft”
- Right Click on Microsoft –> New –> Key and name key to “Office”
- Right Click on Office –> New –> Key and name key to “Outlook”
- Right Click on Outlook –> New –> Key and name key to “AddIns”
- Right Click on AddIns –> New –> Key and name key to “MyFirstAddIn”.

(9) After creating your Registry key “MyFirstAddIn”, follow steps given below to add your values.

- Right Click on Registry Key MyFirstAddIn –> New –> String Value and name it “Description”, Right Click on Description –> Property Window –> Value –> “My First Outlook Addin”
- Right Click on Registry Key MyFirstAddIn –> New –> String Value and name it “FriendlyName”, Right Click on FriendlyName –> Property Window –> Value –> “MyFirstAddIn”
- Right Click on Registry Key MyFirstAddIn –> New –> DWORD Value and name it “LoadBehavior”, Right Click on LoadBehavior –> Property Window –> Value –> “3”
- Right Click on Registry Key MyFirstAddIn –> New –> String Value and name it “Manifest”, Right Click on Manifest –> Property Window –> Value –> “[TARGETDIR]MyFirstAddIn.vsto|vstolocal”

That’s it for your first setup project of your Outlook Add-in, navigate to your Add-in's bin –> Debug –> Click On “MyFirstSetup” or “Setup” and follow installation step. After installation completed, restart your Outlook and on start it will ask you to Install our Add-in into outlook. Click on Install. After installation, it will prompt you with “hi” Message Box. So, every time you open up your Outlook it will prompt with “hi” Message Box. This way you can put your own code in event start up and create setup for that and after installing it you can achieve your goal.


Wroking with Active Directory and Some Error Resolution


Active Directory 
(1) Active Directory and .NET


(2) Howto: (Almost) Everything In Active Directory via C#


(3) Active Directory With C#


(4) Active Directory Display all properties in a table


(5) Integrating LDAP Active Directory into your ASP.Net Web Portal (C# or VB.NET)


Active Directory Tips
(1) Changes Between IIS 6.0 and IIS 7 Security : Migration from IIS 6.0 : Planning Your IIS 7 Architecture : The Official Microsoft IIS Site


(2) Understanding Built-In User and Group Accounts in IIS 7 : Configuring Security : Installing and Configuring IIS 7 : The Official Microsoft IIS Site


(3) Troubleshooting Tips


(4) Resetting Passwords in Active Directory with C# ASP .NET


(5) Active Directory user password expiration date .net /OU Group Policy


Error Troubleshooting With Active Directory
(A) Sharepoint: An operations error occurred
(1) An Operations Error Occured


(2) Impersonation in ASP.NET causes [ COMException (0x80072020): An operations error occurred. 


(3) DirectorySearcher on Sharepoint: An operations error occurred


(B) The attribute syntax specified to the directory service
(1) The attribute syntax specified to the directory service... RSS


(C) a device attached to the system is not functioning. active directory
(1) creating a user in Active Directory: A device attached to the system is not functioning


(2) Samba with active directory groups with special character


(3) Object names


(4) How to add a user in Active Directory with name longer than 20 characters?


(D) The server is not operational.
(E) Exception has been thrown by the target of an invocation.

Monday, March 12, 2012

Working with Masterpages in Sharepoint

Here is some links that will show how to inline code for Masterpage in sharepoint. Also, if you find "Code Block are not allowed in this file" error while you are adding some code blocks to your sharepoint master page follow links given below.