Thursday, December 15, 2011

Set/Update Content Type of new upload file in document library

using (SPSite site = new SPSite(SiteSubSite))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
//Create file Stream object using save local file
FileStream fstream = File.OpenRead(sourceFilePath);
byte[] content = new byte[fstream.Length];
fstream.Read(content, 0, (int)fstream.Length);
fstream.Close();

SPFile file = web.Files.Add(targetDocumentLibraryPath, content, true);

SPContentType spPdf = file.Item.ParentList.ContentTypes[ContentType.Name.ToString()];

file.Item["ContentTypeId"] = spPdf.Id;
file.Item["Content Type"] = spPdf.Name;
file.Item.SystemUpdate();
file.Update();

}
}

Error: "Cannot open database "WSS_Content_...." requested by the login. The login failed. Login failed for user ... "

Today I am converting word to PDF file using Word Automation Service, its throw error like "Cannot open database "WSS_Content_...." requested by the login. The login failed. Login failed for user ... "

Solution :

I have open IIS manager and click on the Application pool. Selected respected site's pool and apply valid user authentication over there. After reset IIS and checked my feature.

Its Working...

Enjoy...

Sunday, December 11, 2011

Enable Riboon Button for specific list and single item selection


<CommandUIHandlers>
<CommandUIHandler Command="ListDocumentMerge_Test_Button" CommandAction="~site/_layouts/ListDocumentMerge/DocumentMerge.aspx?List={ListId}&ID={SelectedItemId}"
EnabledScript="javascript:function singleEnable() {
var items = SP.ListOperation.Selection.getSelectedItems();
var ci = CountDictionary(items);
var listId = SP.ListOperation.Selection.getSelectedList();
if (ci == 1 && listId == '{B8701B6B-8CB7-4105-852F-4C685F580A63}')
{ return true; }
return false; }
singleEnable();" />
</CommandUIHandlers>

Programmatically add Custom action in SharePoint 2010

If you need to create a ribbon button that applies to a specific list in a farm deployment scenario you must write some code in a feature event receiver to create an SPUserCustomAction class as so:
SPList list = web.Lists["SomeList"];SPUserCustomAction action = list.UserCustomActions.Add();action.Title = "Do Something";action.Description = "Sample ribbon button";action.ImageUrl = "/_layouts/images/centraladmin_backupandrestore_granularbackup_32x32.png";action.Location = "CommandUI.Ribbon.ListView";action.Sequence = 0;action.Url = "javascript:DoAction();";action.CommandUIExtension =@"<CommandUIExtension xmlns='http://schemas.microsoft.com/sharepoint/'> <CommandUIDefinitions> <CommandUIDefinition Location='Ribbon.Documents.Manage.Controls._children'> <Button Id='Sample.DoSomething.Button' Command='Sample.DoSomething' Image32by32='/_layouts/images/centraladmin_backupandrestore_granularbackup_32x32.png' Image16by16='/_layouts/images/centraladmin_backupandrestore_granularbackup_32x32.png' Sequence='0' LabelText='Do Something' Description='Sample ribbon button' TemplateAlias='o1' /> </CommandUIDefinition> </CommandUIDefinitions> <CommandUIHandlers> <CommandUIHandler Command='Sample.DoSomething' CommandAction='javascript:DoAction();' EnabledScript='javascript:EnableDoAction();'/> </CommandUIHandlers></CommandUIExtension>";
action.Update();

Hide Ribbon in SharePoint 2010

f you have a to disable / hide the SharePoint 2010 ribbon in list forms (such as New / Edit / Display forms), one of the easiest ways is to do it through CSS.

As SharePoint 2010 provides great flexibility with list forms, we can easily modify whatever is required in a particular form for a particular list.

Click "Edit List" icon in the ribbon and it will fire up SharePoint Designer. Open the form where the ribbon needs to be hidden, right click it and choose "Edit File in Avdanced Mode".

The below is div in which the ribbon resides.
<div id=”s4-ribbonrow” class=”s4-pr s4-ribbonrowhidetitle”>
...
</div>
Now to hide the ribbon, add this to the page.
<style type="text/css">
#s4-ribbonrow{ display:none; }
</style>

Custom Ribbon Menu in SharePoint 2010

Feature.xml

<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
Id="031FE880-77CF-4A84-8C5C-7324229EDC6D"
Title="List Document Merge Custom Ribbon Button"
Scope="Web"
Description="List Document Merge Custom Ribbon Button"
ImageUrl="DOC32.GIF">

<ElementManifests>

<ElementManifest Location="RibbonButton.xml"/>

</ElementManifests>

</Feature>

RibbonButton.xml

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

<CustomAction
Id="ListDocumentMergeCustomRibbonButton"
RegistrationId="101"
RegistrationType="List"
Location="CommandUI.Ribbon"
Sequence="5"
Title="Merge Documents"
Rights="ViewListItems" xmlns="http://schemas.microsoft.com/sharepoint/"
>
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition Location="Ribbon.Documents.Manage.Controls._children">
<Button
Id="Ribbon.Documents.New.ListDocumentButton"
Alt="Merge Documents"
Sequence="5"
Command="ListDocumentMerge_Test_Button"
Image32by32="/_layouts/images/DOC32.GIF"
Image16by16="/_layouts/images/DOC32.GIF"
LabelText="Merge Documents"
TemplateAlias="o1" />
</CommandUIDefinition>
</CommandUIDefinitions>

<CommandUIHandlers>
<CommandUIHandler
Command="ListDocumentMerge_Test_Button"
CommandAction="~site/_layouts/ListDocumentMerge/DocumentMerge.aspx?List={ListId}&ID={SelectedItemId}" />
</CommandUIHandlers>

</CommandUIExtension>
</CustomAction>

</Elements>

Wednesday, December 7, 2011

The type ‘System.IO.Packaging.Package’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘WindowsBase’

While I was working with the Open Office XML SDK 2.0, after I added the reference to the DocumentFormat.OpenXml assembly and tried to build the application I ran into this error message.

The type ‘System.IO.Packaging.Package’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35′

Turns out, all you need to do to fix it is to just add another reference to WindowsBase assembly that is found in the .NET Tab.