Saturday, July 10, 2010

Deleting webparts programmatically in a page, MOSS 2007 (Sharepoint)

Simple way to remove webpart in a page programmatically. [Here I am trying to remove all webparts which are added to 'dafault.aspx' page when I created to Report site]
[SPLimitedWebPartManager  is under "microsoft.sharepoint.webpartpages" namespace]

SPLimitedWebPartManager reportWebPartManager = sourceWeb.GetLimitedWebPartManager("Pages/default.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

int webPartCount = reportWebPartManager.WebParts.Count;
for (int i = webPartCount - 1; i >= 0; i--)
{
reportWebPartManager.DeleteWebPart(reportWebPartManager.WebParts[i]);
}
reportWebPartManager.Dispose();

Adding "Report viewer" webpart programmatically , sharepoint 2007

I know there are many people who are trying to find the solution to add "Report viewer webpart programmatically" to sharepoint 2007 pages. Ok, don't waste time anymore, I will show you how to do that.

STEPS:

1. Open page and manually add "Report Viewer webpart" [this is on "development" machine]
2. After adding webpart find small arrow on right side of web part and click on it to get drop down menu.










3. click on Export and it gives dialogue box to save /open. Save it some location [let's say on Desktop but safe bet is to add to your package folder so that when deployed u can write code to copy this to particular location on production (12 hive folder) then use that location in your code]
4. Now copy and paste the following code.

try

{
using (SPSite sourceSite = new SPSite(siteUrl))
{
using (SPWeb sourceWeb = sourceSite.OpenWeb())
{
sourceWeb.AllowUnsafeUpdates = true;
SPList publishList = sourceWeb.Lists["Pages"];   //THIS is the list name where your pages are stored. If you stored your pages in different list give that name.
int publishListItemCount = publishList.Items.Count;
for (int i = 1; i < publishListItemCount; i++)
{
SPListItem publishListItem = publishList.Items[i];

publishListItem.File.CheckOut();

SPLimitedWebPartManager WebPartMgr = sourceWeb.GetLimitedWebPartManager(siteUrl + "/Pages/" + publishListItem.File.Name, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\Report_Viewer.dwp");

//I saved to this location and my code copy webpart to this location in production when I run deployment package.

//This is needed when we import 'reportviewer' webpart.
//I copied follwoing code from internet...I really don't have any clue about this
if (HttpContext.Current == null)
{
HttpRequest request = new HttpRequest("", WebPartMgr.Web.Url, "");
HttpContext.Current = new HttpContext(request, new HttpResponse(new System.IO.StringWriter()));
HttpContext.Current.Items["HttpHandlerSPWeb"] = WebPartMgr.Web;
}

string errors = String.Empty;
System.Xml.XmlReader xr = System.Xml.XmlReader.Create(new System.IO.StringReader(sr.ReadToEnd()));

System.Web.UI.WebControls.WebParts.WebPart webPart = WebPartMgr.ImportWebPart(xr, out errors);
string zoneId = "Header";
int zoneIndex = 0;
webPart.Title = publishListItem.Title;
WebPartMgr.AddWebPart(webPart, zoneId, zoneIndex);
sr.Dispose();

publishListItem.File.CheckIn("Added 'Report viewer' webpart");
publishListItem.File.Publish("Added 'Report viewer' webpart");
}

sourceWeb.AllowUnsafeUpdates = false;
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}


You are done........open your pages and you see webparts are added to your pages !!!!

Display dynamic Sub menu under Navigation Node

<SharePoint:AspMenu ID="CurrentNav" runat="server" datasourceID="SiteMapDS"
orientation="Vertical"
StaticDisplayLevels="1"
MaximumDynamicDisplayLevels="1"

dynamic menu appears on the right whenever you place mouse on left side node (node with black arrow)


Even this is simple concept but believe me many people miss this. So I am just adding this post so that it may help someone ....

Saturday, May 1, 2010

How to add Custom FIELDS/Columns to Contenttype/LIST programmatically , SHAREPOINT MOSS 2007

I know there are many people who are trying to find a way to add "Custom columns" to contenttypes/lists programmatically ...I spent reasonable time to figure it out so I would like to share it so it may be useful for others...

Ok, I had 2 cups of coffee and then sat in front of system to start my sharepoint deployment package. Everything is going cool until I try to add my custom column with custom fieldtype to my contenttype and attach that contenttype to list. My contenttype is not attached to list...it says "object reference not set to instance of the object".....bang........some thing wrong...!!

I spent some time then finally found solution !! solution is simple "Don't add custom fields to contenttype. use only that are supported by Sharepoint..." I know what you are thinking now....but hold on a second and let me explain it you clearly

1. First, Add the column to contenttype with the base Fieldtype of your custom fieldtype [every custom fieldtype must derive from sharepoint base fieldtype]
2. Attach contenttype, created in step 1, to your list [or multiple lists].  Here you won't see any error and all contenttypes are attached to Sharepoint Lists successfully. So far so good.
3. Now, change the base field type to your custom Field type and update it........then it updates all lists/contenttypes !!!!

I am providing sample code......for STEP 3 [I am assuming you did step 1 and step2]

private void UpdateFieldToCustomFieldType(Guid siteCollectionID, string customFieldType, string fieldName, bool hideField)

{
using (SPSite site = new SPSite(siteCollectionID))
{
using (SPWeb web = site.OpenWeb())
{
if (web.Fields.ContainsField(fieldName))
{
try
{
SPField customField = web.Fields.GetFieldByInternalName(fieldName);
customField.SchemaXml = customField.SchemaXml.Replace("Text", customFieldType);  //"Text" is basefieldtype of my customfieldtype which I used in step 1 to add to contenttype
customField.Group = "Custom Columns";
customField.Hidden = hideField;
customField.Update(true);   //this is important to propagate changes to all contenttypes/lists
web.Fields[customField.Id].Update();
}
catch (System.Exception ex)
{
throw (ex);
}
}

}}}



ENjoy...................

Add New toolbar button on Task Form , MOSS 2007

Ok...it took me some time to keep my word, i promised in my previous blog that I am going to write a blog for adding new custom button to Task toolbar with out creating custom forms..!

Lots of blogs I found on internet says that you need to create a custom form if you want to modify something in default forms that generated automatically whenever you create list in sharepoint..but i don't want to follow that procedure because for me that does not make much sense for small modifications...

You may be thinking, is it can be deployed to production? Yes, the procedure I am following is very easy to deploy to production and you DON'T need to have Sharepoint Designer in production.!!

Ok, enough theory now we see how we can do this....

First, if you did not read my previous blog then first read that blog and come back here because I am not going to explain how to create "CustomToolBar.ascx" page.

STEPS TO FOLLOW

1. Now, I am assuming you read my previous blog and created "CustomToolBar.ascx" page and placed it in  right folder.
2. Open the file you created in above step [you can open with Notepad or you can open with Visual studio].
3. Create custom button which we are going to use in the Task form

Code to create Custom Button

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;

namespace MAR.HariCustomButton
{
   public class CustomOKButton : Button
    {
        public CustomOKButton() : base()
        {
        }
        public override string AccessKey
        {
            get
            {
                return base.AccessKey;
            }
            set
            {
                base.AccessKey = value;
            }
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Text = "Custom OK";
            this.UseSubmitBehavior = false;
            this.ID = "saveCustonButton";
            this.CssClass = "ms-ButtonHeightWidth";  //default sharepoint button CSS
            this.Click += new EventHandler(CustomOKButton_Click);

          //SHOW this button in NEW/EDIT forms only...
            if (SPContext.Current.FormContext.FormMode == SPControlMode.New || SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
            {
                this.Visible = true;
            }
            else
                this.Visible = false;
        }
       void CustomOKButton_Click(object sender, EventArgs e)
       {
           try
           {
               //DO WHAT EVER YOU WANT HERE.....................
                SPListItem NewListItem = SPContext.Current.ListItem;
               NewListItem["updateFieldName"] = "FieldValuechanged";
               NewListItem.Update();
           }
           catch (System.Exception ex)
           {
               throw new System.Exception(ex.Message);
           }
       }

    }
}



4.Compile the project and sign the assembly. 'Signing' of assembly is required [to sign, right click on project, go to "properties", Select "Sign" tab and sign it]

5. Now go ahead register the assembly, which you created in above step,  in "CustomToolbat.ascx" page.
<%@Register TagPrefix="MAR" Assembly="MAR.HariCustomButton, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4dakji9c38ec5" namespace="MAR.HariCustomButton"%>

** Version and PublicKeyToken is different for your assembly **

6. find "<wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbl" [if you search you find this tag 2 times so add following code in 2 places]  in "customToolbar.ascx" file. Add your control in  "<Template_RightButtons>" section.



<Template_RightButtons>
<MAR:CustomOKButton accesskey="<%$Resources:wss,tb_save_AK%>" style="display:none" target="_self" runat="server"/>
<SharePoint:SaveButton runat="server" />
<SharePoint:GoBackButton runat="server"/>
</Template_RightButtons>



** When you add your button in <Template_RightButtons>  section then it appears in all lists. So if you want your button to appear on all Lists then you can delete style="display:none" and you are done....!!

7. But, my case is different, I don't want my button to appear in all lists forms so I make it invisible by default so that I can make it visible for particular list. For that you need to follow 2 more steps.

8. Lets say, I have a custom list in sharepoint named as "ShowMyButtonList" and I have following javascript in my "showbutton.js" file.



function showButtoncontrol()    //'saveCustonButton' is the id of my custom button
{
var FieldName = 'saveCustonButton';
var arr = document.getElementsByTagName("input");
for (var i=0;i < arr.length; i++ )
{
if (arr[i].id.indexOf(FieldName) > -1 || arr[i].title.indexOf(FieldName) > -1)
{
var control = arr[i];
control.style.display = 'block';     //when we created button it is 'invisble' by default, make it visible
}
}
}


//Insert javscript based on page
var strHref = window.location.href;
//u can find this from URL when you open your list
if (strHref.indexOf("Lists/ShowMyButtonList/NewForm.aspx") > -1 || strHref.indexOf("Lists/ShowMyButtonList/EditForm.aspx") > -1)
{
_spBodyOnLoadFunctionNames.push("showButtoncontrol");
}



9. Place the javascript file you created in Step 8 in to "12/Template/LayOuts/1033/showbutton.js"

10. Now, very important step....if you followed all my above steps then Enjoy...else repeat step 1 to 9 to make sure everything is correct...

Happy programming.....

Friday, April 16, 2010

Add your own ringtone to your Iphone ! No hacks, no jail breaking ...!!!

First don't be confused by finding this post in software blog....I am developer by profession but gadget freak...!! ok..now we get started
Ever wondered adding your own ring tone to your Iphone and play that ringtone when ur best friend/loved one called !!
It's damn simple [No hacks, No jail break nothing...]

Here is the procedure
1. Open iTunes and connect your iphone
2. Go to Music folder and select the ringtone you want to upload [should be less than 40 sec with 3g/3gs phone] to iphone [this may be in any format as long as it appears in Music folder then thats fine].
3. Now right-click on the song you selected and select "Convert Selection to AAC."
ITunes create a duplicate version with same name.
4. Now you have to delete the old one[which is selected in step 2 and which is still highlighted]. So go ahead and right click the ringtone and select "Delete." BUT click on the "Keep Files" button so that it won't delete from hard disk.
5. Now select the file created in step 3. press CTRL + C to copy and paste it on Desktop [does not matter, u can select any location]
6. File you copied in Step 5 has .m4a extension/format.
7. Now, select the file you copied, in my case it is on desktop, and right click on it. Go to rename and change extension from .m4a to .m4r
8. Now drag this file and drop it in Music folder/ any folder which your iphone uses to sync. Click on 'Sync' button.
9. Now you can see your song in your custom list of your ring tones...!!!

Easy.....!!! REMEMBER STEP 4 IS VERY IMPORTANT OTHERWISE YOU WONT SEE YOUR CUSTOM RINGTONE IN YOUR IPHONE OR UNDER RINGTONES TAB IN YOUR ITUNES...

ENJOY.............

hold on...there is something...now you can assign this to specific contact so that u get ringtone when that person calls...it's simple...
Open your contact, then you see assign tone option, select custom ringtone u want to play...that's it........

Here is another, what if your song is more than 40 sec..[Step 2]! no worries following procedure cuts 40 sec from your entire song using Itunes !!!!!

1.Select the song you want to make into a ringtone.
2.Right-click the song and select "Get Info.".
3.Click "Options" tab.
4.Type start time (minutes: seconds format) in "start time" textbox where u want to start your ringtone.
5.do the same for "stop time" textbox [this is where u want to stop ur ringtone, amke sure ur start and stop time is not more than 40 sec].
9.Click "OK."

Then follow same procedure from Step 3 .........Enjoy

Wednesday, April 7, 2010

Customize List Form Tool bar buttons in Sharepoint...

Ever wondered how to customize Toolbar buttons in list/task forms at site collection level? Life is easy if you know basic sharepoint folder structure!

Here is what you do
1. Go to ControlTemplates under 12 hive folder [C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES]
2. Now create new ascx file under that folder [right click and create txt document then rename it to CustomToolBar.ascx]
3. Now locate "DefaultTemplates.ascx" file under same folder and search for following

4. Copy entire tag
<sharepoint:renderingtemplate id="ListForm" runat="server"> ........ All the text until......</sharepoint:renderingtemplate>

5. Now open the ascx file you created in step 2and paste the code you copied from "defaultTemplates.ascx" in step 4.

6. Now locate "sharepoint:savebutton" in your file. Now you can customize it for example: add Text="Save" so button Text changed from "Ok" to "Save" !!!

you can do the same for sharepoint:gobackbutton

[NEVER MODIFY DefaultTemplates.ascx FILE. CREATE YOUR OWN FILE, SHAREPOINT USES YOUR FILE TO OVERRIDE DEFAULTEMPLATES.ASCX FILE]

7.Also, don't forget to add <assembly> and <page> directives to your custom file from DefaultTemplates.ascx file.

8.Now, You MUST restart IIS to see your changes... [u can use iisreset /noforce from command prompt to restart your IIS]

Enjoy...............

In my next blog..i will show you how to add custom
and handle "click" event for it [something like "Save As Draft" !!!]