Monday, April 30, 2012

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

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();
                           }

                       }
                   }
               }
           });

http://dishasharepointworld.blogspot.com/

Friday, April 20, 2012

Event on Save button click using JQuery in SharePoint Form(New/EditForm.aspx)

Validation on dropdown value in New/EditForm.aspx before submit the Form using JQuery


<script language="javascript" type="text/javascript" src="../../jQuery%20Libraries/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
function PreSaveAction()
{
 if(($("select[title$='Status']").val()=='Select') && ($("select[title$='Status']").is(':visible')))
 {
  alert("Required field: "+ $("select[title$='Status']").attr('title'));
  return false;
 }
return true;
}
</script>
Here PreSaveAction() is the build in function so we need to add our custom code in this function.

Using jQuery to Prefill and Disable Required Columns in a SharePoint Form


<script language="javascript" type="text/javascript" src="../../jQuery%20Libraries/jquery-1.4.2.min.js"></script>

2<script language="javascript" type="text/javascript">
3  $(document).ready(function() {
4    $("input[Title='Title']").val("Hello World");
5    $("input[Title='Title']").attr("disabled", "disabled");
6  });
7  
8  function PreSaveAction() {
9    $("input[Title='Title']").attr("disabled", "");
10    return true;
11  }
12</script>
 
 
********************************************************************
 
if you need to know if a specific object is visible:
if ( $('#myitem').is(':visible')){
// perform logic if the item is visible..
}

************************************************

Tuesday, April 3, 2012

Change Width of the Column SharePoint List using JQuery

Change a column width  using jQuery:

View Source to see the name of the table that contains your target column's
name. In this case my list  has a "Category" column. The table that holds the
column header that I want to alter looks like the following:

<table style="width: 100%;" sortable="" sortdisable="" filterdisable=""
filterable="" name="Category" ctxnum="1" displayname="Category"
fieldtype="Lookup" resulttype=""
sortfields="SortField=Category&SortDir=Asc&View=%7b864B3C2A%2dDF2B%2d4D2\
9%2d9B31%2d9B5191754A80%7d" height="100%" cellspacing="1" cellpadding="0"
class="ms-unselectedtitle" onmouseover="OnMouseOverFilter(this)">

See the name property? It's "Category"

If I created a column  called "Category Column" instead of "Category" it would
show: name="Category _x0020_Column"

From whatever view you want to implement this on (e.g. AllItems.aspx) select
'Site Actions' --> Edit Page. Add a Content Editor Web Part (CEWP) and add the
following to the Source Editor:

<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
$(function(){

$("table[name=Category]").css("width","300px");

});
</script>

or

<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js“></script>
< script>
$(function(){
$(“tr.ms-viewheadertr th:contains(‘Title’)”, “#MSO_ContentTable”).css(“width”, “50px”);
$(“tr.ms-viewheadertr th:contains(‘Assigned To’)”, “#MSO_ContentTable”).css(“width”, “300px”);
});
< /script>

Monday, April 2, 2012

Clearing the SharePoint Designer 2010 workflow assembly cache

To clear the SPD activity cache on Windows7:
 
 C:\Users\\AppData\Local\Microsoft\WebsiteCache and delete all items in the folder