A Practical Guide to SharePoint 2013

A Practical Guide to SharePoint 2013
A Practical Guide to SharePoint 2013 - Book by Saifullah Shafiq

Thursday, September 14, 2006

Text functions for "calculated" field

Q:  Is it possible to get the first letter of a column in calculated field?
Use the Left function to get the first letter.

=LEFT(Column1,1) -> This will return the first character of column1
Instructions:
Suppose you want to get the first letter of "Title" field in document library.
1. Add a new column.
2. Give a name to the new column and select "Calculated" as the type.
3. Type the following formula in the formula box:
    =Left(Title,1)
4. Keep the return data type as "Single line of text" and click "OK".

Wednesday, September 6, 2006

Fixing InfoPath Forms in Application Templates (by Bil Simser)

Many users including my students frequently ask me how to fix this issue and i always forget the URL. Googling for the following specific link always took my time so this time i have decided to post it on my blog so that next time if some one asks, it will be easier for me to point him/her in the right direction.

http://weblogs.asp.net/bsimser/archive/2005/08/09/421993.aspx (A great post from Bil Simer!!)

Regards,

-SSA

Fixing InfoPath Forms in Application Templates (by Bil Simser)

Many users including my students frequently ask me how to fix this issue and i always forget the URL. Googling for the following specific link always took my time so this time i have decided to post it on my blog so that next time if some one asks, it will be easier for me to point him/her in the right direction.

http://weblogs.asp.net/bsimser/archive/2005/08/09/421993.aspx (A great post from Bil Simer!!)

Regards,

-SSA

Creating trust relationship in Windows

SharePoint is installed in Domain1 and you want to allow Domain2 users to access SharePoint. Both domains are in different forests. The solution is to create a 1 way trust between the two domains. Domain2 should trust Domain1 which is in login forest. To learn how to create trust relationship, see the following article:

http://www.windowsnetworking.com/articles_tutorials/Creating-Trusts-Between-Forests.html

Sunday, September 3, 2006

Updating Locked Files in SharePoint (using .NET Code)

Audience: Intermediate
Keywords: SharePoint, File lock

Q: When i try to update or delete a newly inserted file using .NET code, i get an error. SharePoint keeps the file locked for editing. I try to update file in the OnInsert() function.
Yes, this is true. SharePoint keeps your file locked. Easy solution is to rename the newly inserted document and then do manipulations on it. Interestingly, SharePoint won't allow you to rename the newly inserted file. So what should you do then? Create a copy of the newly inserted document with a different name and then you can update it's metadata easily. The other solution is given below but this won't work if you are working on a machine other than the server so this one is not very helpful but I am including it here for your learning.
NOTE: This code will not work if you are trying to update/delete file from a client machine.
Try this code:

-----------
spFile = EventWeb.GetFile( EventFileUrl );
spListItem = spFile.Item;
spListItem.ListItems.List.Items.DeleteItemById(spListItem.ID);
--------------
spListItem must be defined in the declaration section of EventSink class as following:
private SPListItem spListItem = null;

Be warned that if the file is open or if you have just finished some processing on the file then SharePoint won't let you delete or update it. The file is locked by SharePoint. If you are running the application on the SharePoint machine then you can use the following code to wait for SharePoint to release the lock on the file:

 private bool IsReadyToUpdate(string sDocName)
  {
   Process [] pWord = Process.GetProcessesByName("winword");

   int i ;
   if (pWord.Length >= 1)
   {
    for (i=0;i< pWord.Length;i++)
    {
     //pWordIdea.WaitForInputIdle();
     writeToEventLog("Status: Document is being added from machine: " + pWordIdea.MachineName.ToString()); 
     if (pWordIdea.MainWindowTitle.ToString()  == sDocName + " - Microsoft Word")
     {
      writeToEventLog("Status: Document "" + sDocName + "" found to be open. Waiting for it to be closed ...");
      
      if (!pWordIdea.HasExited)
      {
       pWordIdea.WaitForExit();
      }
      
      writeToEventLog("Status: Document "" + sDocName + " " closed down.");
      break;
     
     }
     else if (pWordIdea.MainWindowTitle.ToString()  == "Document1 - Microsoft Word")
     {
      writeToEventLog("Status: Document1 found to be open. Waiting for it to be closed ...");
      //if (!pWordIdea.HasExited)
      if (!pWordIdea.HasExited)
      {
       pWordIdea.WaitForExit();
      }
      writeToEventLog("Status: Document1 closed down.");
      break;
     }
     else
     {
      pWordIdea.CloseMainWindow();
      break;
     }
    
    }
    return true;
   }
   else
   {
    return true;
   }
  }   

 
WriteToEventLog() writes message in the Windows Log file. You can replace this function with your own code.
Here is another work around for this problem:
if (listEvent.Type == SPListEventType.Insert)
{

WindowsImpersonationContext wic = WindowsIdentity.GetCurrent().Impersonate();

SPWeb web = listEvent.Site.OpenWeb();

SPFile file = null;
SPListItem item = null;

file = web.GetFile(listEvent.UrlAfter);
item = file.Item;
item.ModerationInformation.Status = SPModerationStatusType.Pending;
item.Update();

wic.Undo();

}

Following snippet was sent by a friend. It is similar to the code shown above but contains error handling as well:
SPListItem item = listEvent.Site.OpenWeb().GetFile(listEvent.UrlAfter).Item;
//I think it's not a good method using while to check locked file.
String errorMsg = "Document Locked";
while(errorMsg.IndexOf("Document Locked")!=-1 || errorMsg.IndexOf("Save Conflict")!=-1)
{
Thread.Sleep(1000);
errorMsg = "";
item = listEvent.Site.OpenWeb().GetFile(listEvent.UrlAfter).Item;
//do something
  try
  {
    //update doc right now
    item.Update();
   }
   catch(Exception e)
   {
errorMsg = e.Message;
EventLog.WriteEntry("update Item", e.Message + "||" + e.StackTrace, EventLogEntryType.Error, 1);
   }
}

-SSA

Sending an email to all members of the site using .NET code

Category: Development
Level: Beginner 
You can send an email to all members of your SharePoint site using the following code:

SPSite mySite;
SPWeb currentWeb;
mySite = new SPSite(Url); //Url contains site URL
currentWeb = mySite.OpenWeb();
string sMemberName = "";
string strSubject = "";
string strBody = "";
for (int i=0;i<currentWeb.Users.Count;i++)
{
 sMemberName = currentWeb.UsersIdea.LoginName.ToString();
 strSubject = "Testing";
 strBody = "This is a test.";
 SendMail(currentWeb,currentWeb.Users[sMemberName].ToString() , strSubject, strBody);
}
//SendMail Function
public void SendMail(SPWeb currentWeb, string _user,string strSubject, string strBody)
{
 
 MailMessage msg;
 try
 {
  SmtpMail.SmtpServer = "smtp.yourserver.com";
  msg = new MailMessage();
  msg.BodyFormat = MailFormat.Html;
  msg.From =  currentWeb.Author.Email;
  msg.Subject = strSubject;
  msg.Body = strBody;
  msg.To = currentWeb.Users[_user].Email;
  SmtpMail.Send(msg);
    
 }
 catch (Exception ex)
 {
  //Exception Handling Code ...
 }
}

--SSA

Friday, September 1, 2006

Setting up SharePoint Virtual Development Environment by Bil Simser

This guide will help you in setting up a virtual SharePoint environment.

-SSA

SPS 2003: "can't move focus to the control" error

Read the following article if you are receiving the "can't move focus to the control" javascript error:

http://support.microsoft.com/?kbid=831107&FR=1

You get this error when you add a new required column in your document library and try to approve an old document that already existed there before the new column was added. I noticed this problem today when i was experimenting with an automated system. The application tried to upload the document to the library and succeeded but when i went to the library and tried to approve it, i got the javascript error. The solution is to edit the document properties, fill in the required field and then approve it.

Regards,

-SSA