October 31, 2013

Convert text files between character sets

Hello Friends,

I was trying to upload the Bulk data from Flat text file to SQL Table, but it was required to have character sets as ANCII.. but when we manually change some data in text file it is keeping first column first value as null..

So after spending time on it, I found that file has to be converted to ANCII format..

Here is the commands for same..

On Windows with Powershell:

PS C:\> gc -en utf8 'in.txt' | Out-File -en ascii 'out.txt'

(No ISO-8859-15 support though; it says that supported charsets are unicode, utf7, utf8, utf32, ascii, bigendianunicode, default, and oem.)

CsCvt - Kalytta's Character Set Converter (http://www.cscvt.de) is another great command line based conversion tool for Windows.


Enjoy...

September 19, 2013

Display Item Attachments in SharePoint List View

It is very easy to display list item attachments
in List View and also manipulate the display of attachment, like if attachment is
image, we can display image not just a link.


1. Open List View in SharePoint Desinger
2. Add new column titled "Attachments".

3. Select TD tag of New column.


4. Replace selected code with below code.


 <td id="ItemAttchment" class="ms-vb">
          <xsl:element name="SharePoint:AttachmentsField">
          <xsl:attribute name="runat">server</xsl:attribute>
          <xsl:attribute name="FieldName">Attachments</xsl:attribute>
          <xsl:attribute name="ControlMode">Display</xsl:attribute>
          <xsl:attribute name="Visible">true</xsl:attribute>
          <xsl:attribute name="ItemId">
          <xsl:value-of select="@ID"/>
          </xsl:attribute>
          </xsl:element>
</td>

 and also add

<xsl:value-of select="$thisNode/@ID"></xsl:value-of>


5. View with Attachments

Enjoy !!!

August 7, 2013

SharePoint Bulk Delete List Items Programatically

While working on a custom SharePoint solution, I had a requirement to delete multiple list items one time rather than deleting one by one.

Example: Delete only the items from a list which doesn't have value for a custom column.

When i think of the solution i couldn't make new SPListItemCollection object out of the items which needs to be deleted, which is possible in regular c# application development.
However in SharePoint we can do the batch delete of list items using CAML script.
The CAML script needs to be generated for the items which needs to be deleted as below.

Get the list items:
SPListItemCollection items = spList.Items;

Generate CAML Script:

StringBuilder methodBuilder = new StringBuilder();
string batchFormat = "" +
"{0}"; //{0}-methodFormat

string methodFormat = "" + //{0}-Unique Value for each Item
"{1}" + //{1}-List Guid
"{2}" + //{2}-List ItemID
"Delete" +
"
";


// Build the CAML delete command script.
foreach (SPListItem item in items)
{
    //get the custom column value
    string customColumnValue = string.Empty;
    if (null != item["CustomColumnName"])
        customColumnValue = item["CustomColumnName"].ToString();
   
    //check whether custom column is empty
    if (string.IsNullOrEmpty(customColumnValue))
    {
        methodBuilder.AppendFormat(methodFormat, item.ID, item.ParentList.ID, item.ID);
    }
}


//batch delete script. 
string batchDeleteScript = string.Format(batchFormat, methodBuilder.ToString());


Execute CAML Script:

spWeb.ProcessBatchData(batchDeleteScript);

SharePoint Bulk Update List Items Programatically

While working on a custom SharePoint solution, I had a requirement to update multiple list items one time rather than updating one by one.

Example: Update only the items in a list which doesn't have value for a custom column (update with some value).

When i think of the solution i couldn't make new SPListItemCollection object out of the items which needs to be updated, which is possible in regular c# application development.

However in SharePoint we can do the batch update of list items using CAML script.
The CAML script needs to be generated for the items which needs to be updated as below.


Get the list items:

 SPListItemCollection items = spList.Items;
Generate CAML Script:

StringBuilder methodBuilder = new StringBuilder();
string batchFormat = "" +
"{0}"; //{0}-methodFormat
string methodFormat = "" + //{0}-Unique Value for each Item
"{1}" + //{1}-List Guid
"Save" +
"{2}" + //{2}-List ItemID
"{4}" + //{3}-Column Name, {4}-Column Value
"
";


// Build the CAML update command script.
foreach (SPListItem item in items)
{
    //get the custom column value
    string customColumnValue = string.Empty;
    if (null != item["CustomColumnName"])
        customColumnValue = item["CustomColumnName"].ToString();
   
    //check whether custom column is empty
    if (string.IsNullOrEmpty(customColumnValue))
    {
        methodBuilder.AppendFormat(methodFormat, item.ID, item.ParentList.ID, item.ID, "CustomColumnName", "New Updated Value");
    }
}

//batch update script. 
string batchUpdateScript = string.Format(batchFormat, methodBuilder.ToString());


Execute CAML Script:

spWeb.ProcessBatchData(batchUpdateScript);
 

July 11, 2013

Orphan Event Receiver

I was wokring on to remove 'Orphan Event Receiver' without affecting the exitsting application, I research a lot and found one workaround,



Steps to Remove orphan event receiver,

1)      Use SharePoint Manager Utility, to check is there any event receiver present or not, if present remove the event receiver using SharePoint Manager Utility
2)      Execute following power shell script,
  function Remove-SPFeatureFromContentDB($ContentDb, $FeatureId, [switch]$ReportOnly)
{
    $db = Get-SPDatabase | where { $_.Name -eq $ContentDb }
    [bool]$report = $false
    if ($ReportOnly) { $report = $true }
   
    $db.Sites | ForEach-Object {
       
        Remove-SPFeature -obj $_ -objName "site collection" -featId $FeatureId -report $report
               
        $_ | Get-SPWeb -Limit all | ForEach-Object {
           
            Remove-SPFeature -obj $_ -objName "site" -featId $FeatureId -report $report
        }
    }
}
function Remove-SPFeature($obj, $objName, $featId, [bool]$report)
{
    $feature = $obj.Features[$featId]   
    if ($feature -ne $null) {
        if ($report) {
            write-host "Feature found in" $objName ":" $obj.Url -foregroundcolor Red
        }
        else
        {
            try {
                $obj.Features.Remove($feature.DefinitionId, $true)
                write-host "Feature successfully removed from" $objName ":" $obj.Url -foregroundcolor Red
            }
            catch {
                write-host "There has been an error trying to remove the feature:" $_
            }
        }
    }
    else {
        #write-host "Feature ID specified does not exist in" $objName ":" $obj.Url
    }
}
Remove-SPFeatureFromContentDB -ContentDB "ContentDBName" -FeatureId "FeatureID" –ReportOnly
This Command will report the Feature is present in DB or not.
 
Remove-SPFeatureFromContentDB-ContentDB "ContentDBName"-FeatureId "FeatureID"
This command will remove the Feature from DB

After that check the featuer still existing using 'SharePoint Manager' and if still exisit delete using 'SharePoint Manager'
And reinstall the fearture...
Thanks... if you found any better solution please post.

July 4, 2013

Allow Anonymous users but still SharePoint site popup for Credentials.

With some development part, I came across one issue, I allow Anonymous users on my site, but when I access Portal, still it gives popup for credentials..

I searched all blogs, and check setting everywhere, but unable to find the issue, then last I come across one article in TechNet (Choose security groups) and that help me to resolve the issue.

If in permission for Site in Central Admin, has Deny All; then portal will not behave as Anonymous. So I removed the Deny All permissions and it works..

Here is the article.. (Link)
 
Permission policies provide a centralized way to configure and manage a set of permissions that applies to only a subset of users or groups in a Web application. You can manage permission policy for anonymous users by enabling or disabling anonymous access for a Web application. If you enable anonymous access for a Web application, site administrators can then grant or deny anonymous access at the site collection, site, or item level. If anonymous access is disabled for a Web application, no sites within that Web application can be accessed by anonymous users.
  • None No policy. This is the default option. No additional permission restrictions or additions are applied to site anonymous users.
  • Deny Write Anonymous users cannot write content, even if the site administrator specifically attempts to grant the anonymous user account that permission.
  • Deny All Anonymous users cannot have any access, even if site administrators specifically attempt to grant the anonymous user account access to their sites.
Hope it will help...

 

January 7, 2013

Invalid URI: The URI is Empty while Activating K2 Features from Centrial Administration

Hello All,

I was working with some exiting application where K2 and SharePoint is already in use. But due to some reason ‘Activate All K2 Features’ and ‘K2 Configuration Settings from Central Administration is giving error: ‘Invalid URI: The URI is empty. for New Web Applications.

I had done research and come to know that, when we create new Web application and new site collection, that site URL has to be added into the K2 Workspace which has mapped in SQL database of Environment Field Values.

Somehow this SharePoint Site Web Application URL is not added in to it, to the SharePoint Site variable, it was giving error, so for temporary fix we can add the URL to that variable in K2 Workspace.

This is manual efforts so If anyone has any better solution please post or reply...