Friday, March 30, 2012

Customize the Homepage of SharePoint Blog Sites for Blog Posts Webpart

Hello Friends

The purpose of today Blog is how we can customize view of Posts for Blog Site.
Actually I want to add the new fields on the homepage of the blog site template’s summary view web part for each of the posts.

Let us see how can we do this. Here is step by step process to do this

1. I assume that You have already created your blog site, Add custom column inside the Posts List , in my case it is DelegatedBy which is Person or Group field type.

2. I have downloaded this custom blog.xsl file and upload it to a Site Assets or any other Document Library of your choice. Search for “DelegatedBy” where we added this field to the output which is the only customization that was 
performed on the original XSL.

3. Open the blog site homepage and edit it in SharePoint Designer. Here, you’ll want to click on the Posts web part and add the DelegatedBy column for the field to be displayed in the view.



4. We need to include a link reference to the custom XSL that you had uploaded in Step 2.



5. Save changes that you’ve made in SharePoint Designer and browse to your blog site , magic will happen .

Enjoy Blog Site!!!

Thanks
Disha Shah


Wednesday, March 14, 2012

Apply CSS for SPGridView Programatically in Webpart

Hello Friends

I have been working on webpart in which I was using SPGridView. Now I want to apply  fore-color to one column inside the SPGridView. When I tried with everything and none of them was working , every time it gets color from CSS file then I tried below line it works like magic!!!

oGridColCourse.ItemStyle.CssClass = "color: #0000F5 !important;";

Happy Coding!!!
Disha Shah

Tuesday, March 6, 2012

Move/Migrate SharePoint List Attachments to Document Library with Created and Modified Date

Hello

We have faced one problem about SharePoint Search . We have SharePoint list in which we have items and it has attachments, but SharePoint Search could not look inside Documents which are stored as Attachments in List. So to make it work we have to move all attachments inside the document library so SharePoint Search can crawl and return the results :).

So to provide Keyword Search we need to Move the SharePoint Attachments to Document Library so that we can search in SharePoint Search.

Here is code which will move SharePoint List Attachments to Document Library.

SPSecurity.RunWithElevatedPrivileges(delegate()
           {
               using (SPSite site = new SPSite("SiteURL"))
               {
                   SPList docDestination = site.RootWeb.Lists["DocName"];
                   SPFolder fldRoot = site.RootWeb.Folders[docDestination.Title];
                   SPFileCollection flColl = null;
                   SPList lstAttachment = site.RootWeb.Lists["ListName"];
                   foreach (SPListItem lstItem in lstAttachment.Items)
                   {
                       if (lstItem.Attachments != null && lstItem.Attachments.Count > 0)
                       {
                           foreach (String strName in lstItem.Attachments)
                           {
                               flColl = fldRoot.Files;
                               SPListItem listtem = docDestination.Items.Add();
                               SPFile FileCopy = lstItem.ParentList.ParentWeb.GetFile(lstItem.Attachments.UrlPrefix + strName);

                               string destFile = flColl.Folder.Url + "/" + FileCopy.Name;
                               byte[] fileData = FileCopy.OpenBinary();

                               SPFile flAdded = flColl.Add(destFile, fileData, site.RootWeb.CurrentUser, site.RootWeb.CurrentUser, Convert.ToDateTime(lstItem[SPBuiltInFieldId.Created]), Convert.ToDateTime(lstItem[SPBuiltInFieldId.Modified]));
                               SPListItem item = flAdded.Item;
                               item[SPBuiltInFieldId.Created] = Convert.ToDateTime(lstItem[SPBuiltInFieldId.Created]);
                               item[SPBuiltInFieldId.Modified] = Convert.ToDateTime(lstItem[SPBuiltInFieldId.Modified]);
                               flAdded.Item.Update();
                           }

                       }
                   }
               }
           });

Disha Shah

How to Copy ListItem from One List to Another List

Hello

Here is code how to copy ListItem from one list to another list from eventhandler.
Make sure both list has same List Field Structure.

Here is Code

public override void ItemAdded(SPItemEventProperties properties)
       {

           if (properties.List.Title.Equals("ListA"))
           {
               SPListItem CopyItem = properties.Web.Lists["ListB"].Items.Add();

               foreach (SPField f in properties.ListItem.Fields)
               {
                   //Copy all except attachments.
                   if (!f.ReadOnlyField && f.InternalName != "Attachments"
                       && null != properties.ListItem[f.InternalName])
                   {
                       CopyItem[f.InternalName] = properties.ListItem[f.InternalName];
                   }
               }


               foreach (string fileName in properties.ListItem.Attachments)
               {
                   SPFile file = properties.ListItem.ParentList.ParentWeb.GetFile(properties.ListItem.Attachments.UrlPrefix + fileName);
                   byte[] imageData = file.OpenBinary();
                   CopyItem.Attachments.Add(fileName, imageData);
               }
               CopyItem.Update();

           }
       }

Hope it helps!!!

Disha Shah

Thursday, March 1, 2012

How to get username from JQuery

Hello Friends


Use below Jquery to get the Current User name
var thisUserAccount = $().SPServices.SPGetCurrentUser({
                fieldName: "Title",
                debug: false
});

Use below code to get the ID for Current User
var thisUserAccount = $().SPServices.SPGetCurrentUser({
                fieldName: "ID",
                debug: false
});

Use below code to get the LoginName for Current User
var thisUserAccount = $().SPServices.SPGetCurrentUser({
                fieldName: "Name",
                debug: false
});

Disha Shah

jQuery is undefined in IE / spservices is null or not an object in IE

I just hated this error when I was working with JQuery. It is working perfectly OK with other browsers but in IE it just makes me crazy.

Then I figured out problem and that was the referencing of files.

I was using  jquery-1.6.1.min.js file from Internet and this file jquery.SPServices-0.6.2.min.js from document library.

Actually the problem with those files were I was referring jquery-1.6.1.min.js from Web and jquery.SPServices-0.6.2.min.js  from document Library and then I downloaded the jquery-1.6.1.min.js file and uploaded to document library where I have put the other file. Then it works like charm.

Disha Shah