October 12, 2009

Open Microsoft Application from SharePoint using Javascript

Hi All,

I have some requirments that to open MS-Word, MS- Excel , MS-PowerPoint and MS- Outllook applications from SharePoint enviornment.

This is possible by using "Process" Class in System.Diagonostics as

Process wordApp = new Process();
wordApp.StartInfo.FileName= "WinWord.exe";
wordApp.Start();


But the problem in SharePoint is the Process is started but with the Credentials of "Network Services" so the word application is not visible.
The above solution is working in any normal ASP.Net web application but not in SharePoint. So we try a work around as use javascript and create ActiveX objects and Pass the Application Name as arguments.


TheCode is as ...
For MS-Word Application.

var WordApplication=new ActiveXObject("Word.Application");
WordApplication.Visible=true; // "Visible" is in the Word Object Model
WordApplication.Documents.Add();


For MS-Excel Application.


var ExcelApplication=new ActiveXObject("Excel.Application");
ExcelApplication.Visible=true; // "Visible" is in the Excel Object Model
ExcelApplication.Workbooks.Add();


For MS-PowerPoint Application.

var PPTApplication=new ActiveXObject("Powerpoint.Application");
PPTApplication.Visible=true; // "Visible" is in the PowerPoint Object Model
PPTApplication.Presentations.Add();


For MS-OutLook Application.

var objO = new ActiveXObject('Outlook.Application');
var objNS = objO.GetNameSpace('MAPI');
var olInbox = objNS.GetDefaultFolder(6);
olInbox.Display();


Try this code in Script.


Prerequsite :


There some prerequsites for this application,
  1. The application is installed at client machine, because it will open client local application but not on servers application.
  2. There is settings for running this scripts as,
Tools > Internet Options… > choose tab Security > click button Custom level > scroll > to 'Run ActiveX controls and plug-ins' > select ‘prompt’ or ‘enable’.

October 8, 2009

Programmatically Adding, Deleting, Copying and Downloading Attachments in SPList

I developed a new functionality in our application where I worked with attachments in SharePoint Lists programmatically… It was a nice learning… Let me share my experiences with that too………… And now I seem to have found an informative stuff for my readers and an apt title too

ADDING AN ATTACHMENT TO AN ITEM IN SPLIST :

private void AddNewAttachment(int NodeID)
{
try
{
SPList myList = SPContext.Current.Web.Lists["Item List"];SPListItem myNewItem = myList.GetItemById(NodeID);
if (fileUpload.PostedFile != null && fileUpload.HasFile)
{
Stream fStream = fileUpload.PostedFile.InputStream;
byte[] contents = new byte[fStream.Length];fStream.Read(contents, 0, (int)fStream.Length);fStream.Close();fStream.Dispose();
SPAttachmentCollection attachments = myNewItem.Attachments;string fileName = Path.GetFileName(fileUpload.PostedFile.FileName);attachments.Add(fileName, contents);
myNewItem["Attached FileName"] = fileName; // store the name of the file in a column for future requirements
myNewItem.Update();
}
}
catch (Exception eAdd)
{
string errAdd = eAdd.Message;
}
}


DELETING AN ATTACHMENT FROM SPLIST :


private void DeleteAttachment(int NodeID)
{
try
{
SPList myList = SPContext.Current.Web.Lists["Item List"];
SPListItem delItem = myList.GetItemById(NodeID);
SPAttachmentCollection atCol = delItem.Attachments;
if (delItem["Attached FileName"] != null)
{
string strFileName = delItem["Attached FileName"].ToString();
delItem["Attached FileName"] = string.Empty;
atCol.Delete(strFileName);
delItem.Update();
}
}
catch (Exception eDel)
{
string errDel = eDel.Message;
}
}



DOWNLOADING THE ATTACHMENT :
Find the download link first then reedirect to another aspx page so that the response ending on the current page does not affect the functionalities on this :-

private void DownloadAttachment(int NodeID)
{
try
{
string AttachmentURL = string.Empty;
SPList myList = SPContext.Current.Web.Lists["Item List"];
SPListItem attItem = myList.GetItemById(NodeID);
if (attItem["Attached FileName"] != null)
{
AttachmentURL = “/Lists/Item%20List1/Attachments/” + NodeID.ToString() + “/” + attItem["Attached FileName"].ToString();

System.Web.HttpContext.Current.Session["FileName"] = attItem["Attached FileName"].ToString();
System.Web.HttpContext.Current.Session["Attachment"] = AttachmentURL.Trim();
}
else
{
lblReport.Text = GetMessage(110);
}
if (AttachmentURL != string.Empty)
Page.Response.Write(”");
}
catch (Exception eDwn)
{
string errDwn = eDwn.Message;
}
}
At the aspx page you need to run the code as :

if(System.Web.HttpContext.Current.Session["Attachment"] != null)
{
string strName = System.Web.HttpContext.Current.Session["FileName"].ToString();
string sbURL = System.Web.HttpContext.Current.Session["Attachment"].ToString();
System.Web.HttpResponse response;
response = System.Web.HttpContext.Current.Response;
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
response.AppendHeader(”Content-disposition”, “attachment; filename=” + strName);
response.AppendHeader(”Pragma”, “cache”);
response.AppendHeader(”Cache-control”, “private”);
response.Redirect(sbURL);
response.End();
}

Now set these sessions to null.


COPYING AN ATTACHMENT FROM ONE ITEM TO ANOTHER IN SPLIST :
private void CopyAttachment(int FromID, int NodeID, string AttachedFile)
{
try
{
SPList myList = SPContext.Current.Web.ParentWeb.Lists["Item List"];
SPListItem myItem = myList.GetItemById(NodeID);
SPListItem myPrevItem = myList.GetItemById(FromID);

SPAttachmentCollection attColl = myPrevItem.Attachments;

SPFile attFile = myPrevItem.ParentList.ParentWeb.GetFile(myPrevItem.Attachments.UrlPrefix + AttachedFile);
string fileRead = myPrevItem.Attachments.UrlPrefix.ToString() + AttachedFile;

StreamReader fsReader = new StreamReader(attFile.OpenBinaryStream());
Stream fStream = fsReader.BaseStream;

byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
fStream.Dispose();

myItem.Attachments.Add(AttachedFile, contents);
myItem.Update();
}
catch (Exception eCopy)
{
string errCopy = eCopy.Message;
}
}


No exact match found when adding using from different domains in SharePoint

Problem:

Either when you’re trying to add a user account from a different domain to a SharePoint site collection or using People Picker you get a “no exact match found”.

Issue:

The service account attached to the Sharepoint Web Application is used for user account verification. In the case where there is only a one-way trust between the domains, a login for the requested domain is required. For example, when you add a user from a different domain to the security list for a file, where the current login does not have permissions to the queried domain a login popup appears. Since a login dialog doesn’t appear for Sharepoint, an additional login is required.

Solution:

In a multi forest network, separate logins will have to be registered against each domain. Assuming you have a login with permissions to the domains that are found in different forests you can run the following command on the Sharepoint server to register the login password (domain:, login, password; domain, login, password):

stsadm.exe -o setproperty -url “<https://SharepointPortal>” -pn “peoplepicker-searchadforests” -pv “domain:na.aecomnet.com,na\mosssp,Password; domain:as.aecomnet.com,nas\mossspas,Password; domain:corp.aecomnet.com,aecom/mossspcorp,Password; domain:au.aecomnet.com,au\mosstest,mos5test”

Deploying .NET Assemblies

I thought I’d cover the intricacies of deploying .NET Dll’s in production and development environments today. One of a developers’ first tasks after creating an assembly is deciding the location to house their newly creating endeavour. The three main ways are

1. Global Application Cache (GAC)
2. Private Assemblies
3. Codebase


Global Application Cache


Storing your assembly in the “Global Application Cache” (GAC) , allows your assemblies functionality to be utilized across several different applications. Once the assembly is in the GAC, the CLR (by default) will search this universal location for the applicable assembly.It is the first location the CLR will search for assemblies.

The location of the GAC is C:\Windows\Assemblies\GAC. There are a number of ways to deploy your assembly to the GAC. The main way is by calling the command line utility GACUtil.exe

Advantages:
1. Shared between applications.
2. Eliminates dependency on relative/absolute path referencing correctness.
3. Small load performance boost (Load time strong naming verification not required as it’s a pre-requisite for GAC deployment) .

Disadvantages:
1. Requires at least power user access control to deploy to GAC.
2. Requires assembly strong naming.

Private Assemblies

The developer’s application directory is also know as the ApplicationBase, and is the private location where the CLR searches for assemblies.

Advantages:
1. XCopy deployment (copy application assemblies, config files to a single location for deployment)
2. Flexible options in strong/weak naming and user installation access control

Disadvantages
1. Cannot share private assemblies between applications


Codebase

This property allows the developer to specify a location to find an assembly (e.g. relative paths or web download). If the assembly is downloaded from the network then the assembly is copied and loaded from a location known as the download cache. Note that this location is not the same location as the GAC and hence does not provide the same capabilities such as assembly sharing.

Advantages:
1. Obtain the assemblies over the network
2. Not restricted to ApplicationBase structure

Disadvantages:
1. Important to get correct directory in config file.